[Tkinter-discuss] Pressing buttons
John McMonagle
jmcmonagle at velseis.com.au
Wed Apr 4 05:26:35 CEST 2007
Bob Greschke wrote:
> You want to do something like this:
>
> from Tkinter import *
> Root = Tk()
>
> EntryVar = StringVar()
>
> def doSomething(e = None):
> print EntryVar.get()
> return
>
> Sub = Frame(Root)
> Ent = Entry(Sub, width = 10, variable = EntryVar)
> Ent.pack(side = LEFT)
> Ent.bind("<Return>", doSomething)
> Ent.bind("<KP_Enter>", doSomething) <- for the numeric keypad Enter key
> Button(Sub, text = "OK", command = doSomething).pack(side = LEFT)
> Sub.pack(side = TOP)
>
> Root.mainloop()
>
>
> It is important that the Entry field .pack() be on a line by itself and not
> combined like Entry().pack(), otherwise Ent will be equal to None and the
> bind will not work.
>
> No problem with the English. We'll get through it. :)
>
> Bob
>
Not sure where Bob pulled "variable" out of, but rest assured it's not
an option to the Entry widget.
Something along these lines may help (note I've excluded the Button as
it appears to be redundant in this case):
from Tkinter import *
root = Tk()
def onPressEnter(event):
print event.widget.get() # Get entry widget's string
event.widget.delete(0, END) # Clears the entry widget
e = Entry(root, width=10)
e.pack()
e.bind('<Return>', onPressEnter)
e.bind('<KP_Enter>', onPressEnter)
root.mainloop()
Regards,
John
More information about the Tkinter-discuss
mailing list