Python Tkinter Entry Widget: Restricting text input size

Sunday, May 17, 2009

In my last blog related to Python Tkinter programming, we have seen an example of a Tkinter Entry widget, which is a single line text input box, I was looking for a reasonable enhancement to that code, restricting the text input size, we have seen in many front end applications that this is a common practice, especially when you type details like your login user name, address, SSN and phone number which are finite length text inputs, so one should restrict those input size based on the maximum length that piece of information can have.

I was looking into different properties of Tkinter Entry text widget to see if there is a provision to set that condition, but then found that writing an event handler for the Entry text widget is the only way to do so, the event handler of a Tkinter widget binds a given event to that widget (say a mouse click, text input, mouse move, pressing Enter key, etc) and invokes a given method when that event is encountered, using the bind method of the Tkinter widget, one can easily add an event and associate it with the method to be called, lets see the code.

1.  # File name:
2. # Author: S.Prasanna
4. from Tkinter import *
5. import tkMessageBox
7. def validateTextInputSize(event):
8. """ Method to Validate Entry text input size """
12. if (event.widget.index(END) >= TEXT_MAXINPUTSIZE - 1):
13. event.widget.delete(TEXT_MAXINPUTSIZE - 1)
15. def displayText():
16. """ Display the Entry text value. """
18. global entryWidget
20. if entryWidget.get().strip() == "":
21. tkMessageBox.showerror("Tkinter Entry Widget", "Enter a text value")
22. else:
23. tkMessageBox.showinfo("Tkinter Entry Widget", "Text value =" + entryWidget.get().strip())
25. if __name__ == "__main__":
27. root = Tk()
30. root.title("Tkinter Entry Widget")
31. root["padx"] = 40
32. root["pady"] = 20
34. # Create a text frame to hold the text Label and the Entry widget
35. textFrame = Frame(root)
37. #Create a Label in textFrame
38. entryLabel = Label(textFrame)
39. entryLabel["text"] = "Enter the text:"
40. entryLabel.pack(side=LEFT)
42. # Create an Entry Widget in textFrame
43. entryWidget = Entry(textFrame)
44. entryWidget.bind("<Key>", validateTextInputSize)
45. entryWidget["width"] = 50
46. entryWidget.pack(side=LEFT)
48. textFrame.pack()
50. button = Button(root, text="Submit", command=displayText)
51. button.pack()
53. root.mainloop()
Sample Output:

Figure 1: Tkinter Entry Widget which restricts the input size to 20 characters


The code is quite simple, we create an Entry text widget (line 43) and bind a Key press event (line 44) which would call the validateTextInputSize method (line 10 - 13), which checks the maximum text input size and when that limit is reached, it will delete the text input corresponding to that index (TEXT_MAXINPUTSIZE = 20, line 8).


Anonymous said...

I am an amateur programmer working on a personal program/hobby. Was looking for a solution to limiting length in a text widget. I thought your solution was elegant so I tried it. Worked great. I made one change, binding the event with a lambda command so I could pass a local variable around. Thanks for the solution

Anonymous said...

THANK YOU - However, try changing this .bind function from to , and get rid of the -1 in the TEXT_MAXINPUTSIZE. It will really clean the function up. Great work, taught me a lot.

Anonymous said...

The post above parsed out my \< and \>

Change the Key part of bind to KeyRelease.

Copyright © 2016 Prasanna Seshadri,, All Rights Reserved.
No part of the content or this site may be reproduced without prior written permission of the author.