How To Run Tkinter Function With Both A Key Bind And A Button Command?
Is it possible to run a function with both a button press and an event? When I try to run this function by pressing the button, it, understandably, gives an error TypeError: listF
Solution 1:
Try setting event=None
for the function and then passing event from the bind
only, like:
self.myButton = tk.Button(master, text='ok', command=self.listFiller)
.....
self.myEntry.bind("<Return>", lambda e: self.listFiller()) # Same as lambda e: self.listFiller(e)deflistFiller(self, event=None):
data = self.myEntry.get()
This way, when you press the button, event
is taken as None
. But when bind
is triggered(i.e.,Enter is hit), event
is passed on implicitly(works even if you pass it explicitly) as e
.
So after you have understood how this works behind the hood, now even if you remove lambda
, e
would still get passed as event
as it happens implicitly.
self.myEntry.bind("<Return>",self.listFiller) # As mentioned by Lafexlos
Though keep in mind, when you press a tkinter button, you cannot use any attributes from event
, like event.keysym
or anything, but it would work if you use Enter key.
Post a Comment for "How To Run Tkinter Function With Both A Key Bind And A Button Command?"