python - Passing string of entry to another function with tkinter -
i'm using python3, trying pass user has typed in username , password box login function when press login button. , login functions should pack label of username onto gui.
code:
import tkinter tkinter import * import sys def login_gui(): global login_window global studentid_entry_login usr_login = stringvar() pwd_login = stringvar() login_window=tkinter.tk() login_window.title("login") login_window.geometry("200x200+500+300") username_label_login = tkinter.label(login_window, text="username:").pack() username_entry_login = tkinter.entry(login_window, textvariable=usr_login).pack() password_label_login = tkinter.label(login_window, text="\npassword:").pack() password_entry_login = tkinter.entry(login_window, textvariable=pwd_login).pack() button_login = tkinter.button(login_window, text="login", command = login).pack() login_window.mainloop() def login(): username = usr_login.get() label1 = tkinter.label(login_window, text=username).pack() return login_gui()
traceback:
traceback (most recent call last): file "c:/python33/folder/tkinter-test.py", line 25, in <module> login_gui() file "c:/python33/folder/tkinter-test.py", line 8, in login_gui usr_login = stringvar() file "c:\python33\lib\tkinter\__init__.py", line 254, in __init__ variable.__init__(self, master, value, name) file "c:\python33\lib\tkinter\__init__.py", line 182, in __init__ self._tk = master.tk attributeerror: 'nonetype' object has no attribute 'tk'
you can't create stringvar
unless tk
object has been initialized.
move login_window
line appears before other assignments:
login_window=tkinter.tk() usr_login = stringvar() pwd_login = stringvar()
also, usr_login
won't visible in login
, you'll nameerror
. make global, or consider putting both login_gui
, login
in class, can access common attributes through self
.
Comments
Post a Comment