Hey,
I'm writing some code in python and I'm having trouble when trying to retrieve content of an Entry widget. The thing is: I want to limit the characters that can be typed, so I'm trying to clear the Entry widget when I reach the specific number of characters (2 in this case), but it looks like I always miss the last typed character. I added the lost character in a print to show.
Here's the sample code:
from Tkinter import *
class sampleFrame:
def __init__(self, master):
self.__frame = Frame(master)
self.__frame.pack()
def get_frame(self):
return self.__frame
class sampleClass:
def __init__(self, master):
self.__aLabel = Label(master,text="aLabel", width=10)
self.__aLabel.pack(side=LEFT)
self.__aEntry = Entry (master, width=2)
self.__aEntry.bind('<Key>', lambda event: self.callback(event, self.__aEntry))
self.__aEntry.pack(side=LEFT)
def callback(self, event, widgetName):
self.__value = widgetName.get()+event.char
print self.__value
if len(self.__value)>2:
widgetName.delete(2,4)
root = Tk()
aSampleFrame = sampleFrame(root)
aSampleClass = sampleClass(aSampleFrame.get_frame())
root.mainloop()
Any help will be much appreciated!
Thanks in advance