Tkinter Entry size limit - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Tkinter Entry size limit (/thread-39019.html) |
Tkinter Entry size limit - vman44 - Dec-21-2022 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. RE: Tkinter Entry size limit - menator01 - Dec-21-2022 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() RE: Tkinter Entry size limit - deanhystad - Dec-22-2022 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/2405/docs/tkinter/entry-validation.html https://www.geeksforgeeks.org/python-tkinter-validating-entry-widget/ RE: Tkinter Entry size limit - vman44 - Dec-22-2022 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. |