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: tkinterTextInputVal.py
2. # Author: S.Prasanna
3.
4. from Tkinter import *
5. import tkMessageBox
6.
7. def validateTextInputSize(event):
8. """ Method to Validate Entry text input size """
9.
10. global TEXT_MAXINPUTSIZE
11.
12. if (event.widget.index(END) >= TEXT_MAXINPUTSIZE - 1):
13. event.widget.delete(TEXT_MAXINPUTSIZE - 1)
14.
15. def displayText():
16. """ Display the Entry text value. """
17.
18. global entryWidget
19.
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())
24.
25. if __name__ == "__main__":
26.
27. root = Tk()
28. TEXT_MAXINPUTSIZE = 20
29.
30. root.title("Tkinter Entry Widget")
31. root["padx"] = 40
32. root["pady"] = 20
33.
34. # Create a text frame to hold the text Label and the Entry widget
35. textFrame = Frame(root)
36.
37. #Create a Label in textFrame
38. entryLabel = Label(textFrame)
39. entryLabel["text"] = "Enter the text:"
40. entryLabel.pack(side=LEFT)
41.
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)
47.
48. textFrame.pack()
49.
50. button = Button(root, text="Submit", command=displayText)
51. button.pack()
52.
53. root.mainloop()
Sample Output:

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

Explanation:

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).

3 comments:

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, www.prasannatech.net, All Rights Reserved.
No part of the content or this site may be reproduced without prior written permission of the author.