[Tutor] binding a button to an entry

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[Tutor] binding a button to an entry

ADRIAN KELLY
Hi all, 
Please can anyone tell me how i bind the activation of a button with input from an entry widget. i know i should be using classes etc. but i don't understand them fully yet.. problem here is that no matter what i enter in the entry window it displays as password incorrect.

from Tkinter import *

password="trial"

def reveal():
    """Display message based on password"""
    contents=s
    if contents=="trial":
        print "password correct"
    else:
        print "password wrong"

#main
root=Tk()
root.title("Password entry box")
root.geometry("300x100")
app=Frame(root)
app.grid()

#labels
lbl=Label(app, text="Enter your password: ")
lbl.grid(row=1, column=0)

#create entry widgets
e = Entry(root)
e.grid(row=1, column=1)
s=e.get()

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


root.mainloop()


thanks all,
adrian







 

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] binding a button to an entry

Dave Angel-3
On 05/01/2012 10:40 AM, ADRIAN KELLY wrote:

> Hi all, Please can anyone tell me how i bind the activation of a button with input from an entry widget. i know i should be using classes etc. but i don't understand them fully yet.. problem here is that no matter what i enter in the entry window it displays as password incorrect.
> from Tkinter import *
> password="trial"
> def reveal():    """Display message based on password"""    contents=s    if contents=="trial":        print "password correct"    else:        print "password wrong"
> #mainroot=Tk()root.title("Password entry box")root.geometry("300x100")app=Frame(root)app.grid()
> #labelslbl=Label(app, text="Enter your password: ")lbl.grid(row=1, column=0)
> #create entry widgetse = Entry(root)e.grid(row=1, column=1)s=e.get()
> #create a submit buttonb=Button(root, text="SUBMIT", command=reveal)b.grid(row=0, column=2)
>
> root.mainloop()
>
> thanks all,adrian
>
>

Please post as text;  this program is incomprehensible as viewed in
Thunderbird.

--

DaveA

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] binding a button to an entry

Prasad, Ramit-2
In reply to this post by ADRIAN KELLY
> Hi all,
> Please can anyone tell me how i bind the activation of a button with input
> from an entry widget. i know i should be using classes etc. but i don't
> understand them fully yet.. problem here is that no matter what i enter in
> the entry window it displays as password incorrect.
>
> from Tkinter import *
>
> password="trial"
>
> def reveal():
>     """Display message based on password"""
>     contents=s
>     if contents=="trial":
>         print "password correct"
>     else:
>         print "password wrong"
>
> #main
> root=Tk()
> root.title("Password entry box")
> root.geometry("300x100")
> app=Frame(root)
> app.grid()
>
> #labels
> lbl=Label(app, text="Enter your password: ")
> lbl.grid(row=1, column=0)
>
> #create entry widgets
> e = Entry(root)
> e.grid(row=1, column=1)
> s=e.get()
>
> #create a submit button
> b=Button(root, text="SUBMIT", command=reveal)
> b.grid(row=0, column=2)
>
>
> root.mainloop()

That is because the value of s never changes once it gets assigned.

Change the following in reveal()
contents=s
to :
contents=e.get()

You may want to read Alan Gauld's tutorial (he is contributor to this
list). It covers classes and basic GUI programming.
http://www.alan-g.me.uk/tutor/index.htm 

Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] binding a button to an entry

Alan Gauld
In reply to this post by ADRIAN KELLY
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
http://www.alan-g.me.uk/

_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor