python - In tkinter how do I assign the entry function to a variable -


i trying use in if statment check if username equal accepted answer. used .get() on ent_username try name choosen did not work. it never gets entered username need more code 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 uses ut lbl = tkinter.label(window, text="the game of 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 ent_username info store username = ent_username.get()  #configure window window.configure(background="#a1dbcd")  #basic enter password lbl_password = tkinter.label(window, text="password", bg="#a1dbcd") ent_password = tkinter.entry(window)  #pack password lbl_password.pack() ent_password.pack() #def check if username valid def question():     if username == "louis":         print("you know")     else:         print("failed")  #will make sign button , call question on click btn = tkinter.button(window, text="sign up", command=lambda: question())  #pack buttons btn.pack()  #draw window window.mainloop() 

your issue trying get contents of entry widget when widget created. , empty string. need move .get() inside function gets value when clicking button.

def question():     username = ent_username.get() # value here     if username == "louis":         print("you know")     else:         print("failed") 

or if ent_username.get() == "louis":

you have option use stringvar i've never found need use except optionmenu widget

also, couple of side notes. when using command argument, need lambda when passing in variable, make sure drop ().

btn = tkinter.button(window, text="sign up", command=question) 

and common practice import tkinter tk. way not prefixing tkinter , instead tk. saves typing , space. looks so,

ent_username = tk.entry(window) 

Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -