Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter Entry size limit
#1
I'm writing a program in Tkinter.

One page is a form, where the user inputs various values.
How do I limit an Entry field to a specific size?

For example, I use the following code, so that the user can put in their zip code:

 # Zip code label_for_zipc = Label(main_window, text='Zip code: (First 5 digits)', fg="blue") label_for_zipc.config(font=('helvetica', 16)) canvas.create_window(0.7*hshift*fnl_x, fnl_y+vertShift, window=label_for_zipc) canvas.create_window(0.7*hshift*fnl_x, fnl_y+vertShift+30, window=entry_for_zipc)
The function that includes the above lines includes many other entry fields.
There is a "Proceed" Button, which will write all the input to a file, as well as bringing up another
Tkinter window for more input.

The problem I have is that, even though I limited the field size/length of "entry_for_zipc" to 5,
the user can type more than 5 characters in the entry field.
How do I program it so that the user cannot type more than 5 characters in the entry field?

Thanks.
Reply
#2
You could test the length of the input and throw an error and remind that only 5 characters are allowed.



Quick example

import tkinter as tk from tkinter import messagebox def callback(field, arg): arg = arg.get() if len(arg) != 5: messagebox.showerror('Error', 'field must contain 5 characters') field.delete(0, tk.END) else: messagebox.showinfo('Correct', 'This field has 5 characters') root = tk.Tk() var = tk.StringVar() contain = tk.LabelFrame(root, text='Some label text') contain.pack(fill='both', expand=True) field = tk.Entry(contain, textvariable=var, bd=2) field.pack(fill='x', expand=True) btn = tk.Button(contain, text='Submit', command=lambda: callback(field, var)) btn.pack(side='left', fill='x', pady=5) root.mainloop()
vman44 likes this post
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
You can use a validation callback to limit what is typed in an entry.
import tkinter as tk from tkinter import messagebox class Window(tk.Tk): def __init__(self): super().__init__() label = tk.Label(self, text="Zip Code") reg = self.register(self.zip_validate) self.zip_code = tk.StringVar(self, '') entry = tk.Entry( self, textvariable=self.zip_code, width=7, justify=tk.RIGHT, validate='key', validatecommand=(reg, '%P')) button = tk.Button(self, text='Accept', command=self.submit) button.pack(side=tk.BOTTOM, padx=5, pady=5, expand=True, fill=tk.X) label.pack(side=tk.LEFT, padx=5, pady=5) entry.pack(side=tk.RIGHT, padx=5, pady=5) @staticmethod def zip_validate(zip_code): return zip_code.isdigit() and len(zip_code) <= 5 def submit(self): if len(self.zip_code.get()) < 5: messagebox.showerror('Error', 'Zip code must be 5 digits', parent=self) Window().mainloop()
This example registers a validation callback function named 'zip_validate'. The function is called whenever a key is pressed ('key'). The function is passed a preview of what the entry contents will be if the key press is accepted ('%P'). The validator returns True if the new content is acceptable, else False.

Note that the validate function does not force the zip code to be 5 digits long. '123' is valid. To ensure that the zip code is 5 digits long you would still need some type of form validation very much like in menator's post.

In this example the validate function is a static method, but it can be a standalone function.

Read about validators here:

https://anzeljg.github.io/rin2/book2/240...ation.html
https://www.geeksforgeeks.org/python-tki...ry-widget/
vman44 likes this post
Reply
#4
Thanks everyone. I'll probably try some version of menator's code.
It seems silly to me that Python/Tkinter doesn't have this built-in.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter only storing last element/data from entry widget bertschj1 8 1,866 May-06-2025, 11:54 PM
Last Post: deanhystad
  how to open a popup window in tkinter with entry,label and button lunacy90 1 4,562 Sep-01-2023, 12:07 AM
Last Post: lunacy90
  Auto increament in Entry field. in tkinter GUI cybertooth 7 7,648 Sep-17-2021, 07:56 AM
Last Post: cybertooth
  How to rotate log and limit the size maiya 0 2,577 Aug-29-2020, 12:41 AM
Last Post: maiya
  How to limit the fie size in logging. maiya 2 11,385 Jul-29-2020, 06:49 AM
Last Post: maiya
  size of set vs size of dict zweb 0 2,996 Oct-11-2019, 01:32 AM
Last Post: zweb
  Restrict / Limit variable size mln4python 4 9,694 Aug-13-2019, 07:17 AM
Last Post: mln4python
  CSV file created is huge in size. How to reduce the size? pramoddsrb 0 12,389 Apr-26-2018, 12:38 AM
Last Post: pramoddsrb
  Help pulling tkinter Entry string data Raudert 8 13,008 Jan-12-2017, 11:49 PM
Last Post: Larz60+

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.