[Tutor] binding a button to an entry

Alan Gauld alan.gauld at btinternet.com
Wed May 2 02:28:15 CEST 2012

On 01/05/12 15:40, ADRIAN KELLY wrote:

> Please can anyone tell me how i bind the activation of a button with
> input from an entry widget.

I don;t know what you mean.
Do you want to make the button press when the entry widget changes? Or 
change the entry widget when the button presses? Or just read the entry 
widget content in the button event handler - which is what the code 
below does - kind of...

> i know i should be using classes etc. but i
> don't understand them fully yet..

You can write Tkinter code without classes, its just more difficult
to avoid complexity...

>  problem here is that no matter what i
> enter in the entry window it displays as password incorrect.

> from Tkinter import *
> password="trial"

If you must use global variables (and without classes you probably do)
at least bring them all together so we don;t need to scan up/down to 
find them.

> def reveal():
> """Display message based on password"""
> contents=s
> if contents=="trial":

You don't need contents here, just use e.get() directly

if e.get() == "trial":

> print "password correct"
> else:
> print "password wrong"
> e = Entry(root)
> e.grid(row=1, column=1)
> s=e.get()

You get this before starting the mainloop so any changes will not be 
seen. Thats why its better to do the get in the event handler. And you 
save another unnecessary variable too...

> #create a submit button
> b=Button(root, text="SUBMIT", command=reveal)
> b.grid(row=0, column=2)
> root.mainloop()

Alan G
Author of the Learn to Program web site

More information about the Tutor mailing list