I am making a login and register screen using tkinter. I have a function to make the login and register screen (named form), and this is where the entry widgets are located (login_username and login_password).
Now, if I try to access this data outside the function, I get a NameError which I know is a problem to do with scope.
I do not want to use a global variable, as this is bad practice, but is there any other way solve this without majorly changing the code? Many Thanks :D
Code:
from tkinter import *
def form(master, label_1_text, label_2_text, button_text, commands):
label_hidden_1=Label(master,text="").pack()
label_hidden=Label(master, text="").pack()
new_username_label=Label(master,text=label_1_text,font=('arial', 10, 'bold')).pack()
login_username=Entry(master, width=40).pack()
label_hidden_2=Label(master,text="").pack()
new_password_label=Label(master,text=label_2_text,font=('arial', 10, 'bold')).pack()
login_password=Entry(master,width=40, show='*')
login_password.pack()
login_password.bind('<Return>', lambda e: commands())
label_hidden_3=Label(master,text="").pack()
login_btn=Button(master,text=button_text,width=20,height=1,font=('arial', 10, 'bold'), command=commands).pack()
master.mainloop()
def main():
def login_user(*args, e=None):
print(login_password.get()) # This is where i get and scope error (NameError)
def register_user(*args, e=None):
print(login_password.get()) # And here also
window = Tk()
window.title('Registration')
window.geometry('275x300')
form(window, 'NEW USERNAME', 'NEW PASSWORD', 'REGISTER', register_user)
window = Tk()
window.title('Login')
window.geometry('275x300')
form(window, 'USERNAME', 'PASSWORD', 'LOGIN', login_user)
if __name__ == '__main__':
main()