Reputation: 23
I am trying to use it in a if statment to check if the username is equal to the accepted answer. I used .get() on my ent_username to try to get the name choosen but it did not work. Is it that it never really gets entered as the username will i need to do more code with the button. Please help....
import tkinter
action = ""
#create new window
window = tkinter.Tk()
#name window
window.title("Basic window")
#window sized
window.geometry("250x200")
#creates label then uses ut
lbl = tkinter.Label(window, text="The game of a life time!", bg="#a1dbcd")
#pack label
lbl.pack()
#create username
lbl_username = tkinter.Label(window, text="Username", bg="#a1dbcd")
ent_username = tkinter.Entry(window)
#pack username
lbl_username.pack()
ent_username.pack()
#attempting to get the ent_username info to store
username = ent_username.get()
#configure window
window.configure(background="#a1dbcd")
#basic enter for password
lbl_password = tkinter.Label(window, text="Password", bg="#a1dbcd")
ent_password = tkinter.Entry(window)
#pack password
lbl_password.pack()
ent_password.pack()
#def to check if username is valid
def question():
if username == "louis":
print("you know")
else:
print("failed")
#will make the sign up button and will call question on click
btn = tkinter.Button(window, text="Sign up", command=lambda: question())
#pack buttons
btn.pack()
#draw window
window.mainloop()
Upvotes: 1
Views: 2938
Reputation: 5373
The easiest approach is to associate a variable with the Entry widget. For the variable, you have to use one of the Tkinter variables, and it has to be the tkinter variable associated with that sort of widget. For the Entry widget, you need a Stringvar. See Effbot's third-party documentation for the Entry widget.
username = tkinter.StringVar()
ent_password = tkinter.Entry(window, textvariable=username)
In the event handler question
, you can then access the Tkinter variable's value.
if username.get() == name_you_want:
print "as expected"
That handler function name question
is the correct value for the command
argument, as Summers said:
btn = tkinter.Button(window, text="Sign up", command=question)
Upvotes: 0
Reputation: 5384
Your issue is that you are trying to get
the contents of the Entry widget when the widget is created. And this will always be empty string. You need to move the .get()
inside the function so it gets the value when clicking the button.
def question():
username = ent_username.get() # Get value here
if username == "louis":
print("you know")
else:
print("failed")
Or if ent_username.get() == "louis":
You do have the option to use a StringVar
but I've never found the need to use it except with OptionMenu widget
Also, a couple of side notes. When using the command
argument, you only need lambda
when passing in a variable, just make sure to drop the ()
.
btn = tkinter.Button(window, text="Sign up", command=question)
And a common practice is to import tkinter as tk
. This way you are not prefixing everything with tkinter
and instead tk
. It just saves typing and space. So it looks like so,
ent_username = tk.Entry(window)
Upvotes: 2