Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter Entry size limit
#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


Messages In This Thread
Tkinter Entry size limit - by vman44 - Dec-21-2022, 06:50 AM
RE: Tkinter Entry size limit - by menator01 - Dec-21-2022, 08:15 AM
RE: Tkinter Entry size limit - by deanhystad - Dec-22-2022, 12:08 AM
RE: Tkinter Entry size limit - by vman44 - Dec-22-2022, 06:58 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to open a popup window in tkinter with entry,label and button lunacy90 1 978 Sep-01-2023, 12:07 AM
Last Post: lunacy90
  Auto increament in Entry field. in tkinter GUI cybertooth 7 4,275 Sep-17-2021, 07:56 AM
Last Post: cybertooth
  How to rotate log and limit the size maiya 0 1,790 Aug-29-2020, 12:41 AM
Last Post: maiya
  How to limit the fie size in logging. maiya 2 7,430 Jul-29-2020, 06:49 AM
Last Post: maiya
  size of set vs size of dict zweb 0 2,181 Oct-11-2019, 01:32 AM
Last Post: zweb
  Restrict / Limit variable size mln4python 4 7,282 Aug-13-2019, 07:17 AM
Last Post: mln4python
  CSV file created is huge in size. How to reduce the size? pramoddsrb 0 10,541 Apr-26-2018, 12:38 AM
Last Post: pramoddsrb
  Help pulling tkinter Entry string data Raudert 8 10,151 Jan-12-2017, 11:49 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020