简体   繁体   中英

NameError: name 'username' is not defined in python with tkinter GUI

I get the error I mentioned above for line 28 in the following code. I don't know what the problem is at all. Any help appreciated, this is the code:

#26/4/2020 28/4/2020 30/4/2020

def sign_in_clicked():
    username = username_entry.get()
    password = password_entry.get()
    username_entry.delete(0, END)
    password_entry.delete(0, END)

from tkinter import *

sign_in = Tk()
sign_in.title("Sign in")

sign_in_label = Label(sign_in,text="Please Sign In!").grid(row=0, column=0, columnspan=2, sticky=W)
username_label = Label(sign_in,text="Username:").grid(row=1, column=0, sticky=W)
password_label = Label(sign_in,text="password:").grid(row=2, column=0, sticky=W)

username_entry = Entry(sign_in, width=20, bg="gray70")
username_entry.grid(row=1, column=1, sticky=W)
password_entry = Entry(sign_in, width=20, bg="gray70")
password_entry.grid(row=2, column=1, sticky=W)

sign_in_button = Button(sign_in, text="Sign In", width=25, command=sign_in_clicked).grid(row=3, column=0, columnspan=2, sticky=W)

output_text = Text(sign_in, width=23, height=6, wrap=WORD, background="DarkOrchid1")
output_text.grid(row=6, column=0, columnspan=2, sticky=W)

if username=="1" and password=="2":
    output_text.insert(END, "hi")

sign_in.mainloop()

Variables username and password are not defined.

if username=="1" and password=="2": #here you are referencing non-existant variables
    output_text.insert(END, "hi")

You only create username and password in a function; they are not available outside this function.

You may want to use username_entry.get() and password_entry.get() .

you should write:

def sign_in_clicked():
global username,password
username = username_entry.get()
password = password_entry.get()
username_entry.delete(0, END)
password_entry.delete(0, END)

i think that this will help, if not try to learn more about the global declaration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM