0

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

  • I stated i do not want to do this as it is bad practice – Python Coding Tutorials Jun 27 '21 at 17:14
  • As @ Sujay pointed out, `login_password` only exists in your `form` function, so you need to tell python to make the variable global by using `global login_password` at the start of your `top` function – TheLizzard Jun 27 '21 at 17:14
  • Global var's are usually refer to bad practice, however they are sometimes necessary. A large issue is that you are declaring a variable that you want to persist inside a function. This is fine as long as the variable already exists or if you return the value. – TheLazyScripter Jun 27 '21 at 17:16
  • The only problem with global variables is that you can get variable name clashes if you aren't careful. The best way of approaching this problem would be by using classes. – TheLizzard Jun 27 '21 at 17:16
  • @TheLizzard how could I do this using classes? – Python Coding Tutorials Jun 27 '21 at 17:21
  • There are plenty of examples using classes with tkinter – Delrius Euphoria Jun 27 '21 at 17:23
  • @PythonCodingTutorials Do you know how to use classes? If not, look at *object oriented programming* tutorials. It's too much to explain in a stackoverflow answer. Also it might mean that you need to redesign your code. The advantage is that you aren't going to need any global variables. – TheLizzard Jun 27 '21 at 17:23
  • Check: [How to make 2 functions for 2 tkinter windows more condensed?](https://stackoverflow.com/a/68124161/13382000) – Delrius Euphoria Jun 27 '21 at 17:25

1 Answers1

0

After the reading the comments, I have decided to use classes as TheLizzard and Cool Cloud mentioned. Thanks for the info.