-1

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()

Cerys
  • 1
  • 1
  • 1
    You have defined the username inside the function sign_in_clicked. – Rima Apr 30 '20 at 22:04
  • Does this answer your question? [Using global variables in a function](https://stackoverflow.com/questions/423379/using-global-variables-in-a-function) – tgikal Apr 30 '20 at 22:54
  • You should put the if block inside `sign_in_clicked()` function. – acw1668 May 01 '20 at 01:25

2 Answers2

0

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().

Rafael
  • 525
  • 7
  • 20
0

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.