[英]Why getting error : "home_frame is not defined" even if it's already global?
I'm creating a simple Tkinter GUI app that has a simple login page.我正在创建一个简单的 Tkinter GUI 应用程序,它有一个简单的登录页面。 When user is logged in, home page is opened with three buttons: go to page A, go to page B or logout.当用户登录时,主页会通过三个按钮打开:go 到页面 A,go 到页面 B 或注销。 But I get the error: line 75, in show_login_page home_frame.pack_forget() NameError: name 'home_frame' is not defined但我收到错误:第 75 行,在 show_login_page home_frame.pack_forget() NameError: name 'home_frame' is not defined
Even if home_frame is declared global in line 33.即使 home_frame 在第 33 行被声明为全局的。
Here is my code:这是我的代码:
import tkinter as tk
# Create the main window
window = tk.Tk()
window.title("Login")
window.geometry("300x300")
# Create the login page as a frame
login_frame = tk.Frame(window)
def check_login():
# Get the username and password from the form
username = username_entry.get()
password = password_entry.get()
# Check the login credentials
if username == "1" and password == "1":
# If the login is successful, destroy the login page and show the home page
login_frame.pack_forget()
show_home_page()
print("Yo")
else:
# If the login is unsuccessful, show an error message
error_label = tk.Label(login_frame, text="Invalid login. Please try again.")
error_label.pack()
# Create the submit button
submit_button = tk.Button(login_frame, text="Submit", command=check_login)
submit_button.pack()
# Define the function to show the home page
def show_home_page():
global home_frame
# Create the home page as a frame
home_frame = tk.Frame(window)
home_frame.pack()
try:
page_a_frame.pack_forget()
page_b_frame.pack_forget()
login_frame.pack_forget()
except NameError:
pass
# Create the buttons to navigate to the other pages
logout_button = tk.Button(home_frame, text="Logout", command=show_login_page)
page_a_button = tk.Button(home_frame, text="Page A", command=show_page_a)
page_b_button = tk.Button(home_frame, text="Page B", command=show_page_b)
logout_button.pack()
page_a_button.pack()
page_b_button.pack()
# Define the functions to show the other pages
def show_page_a():
global page_a_frame
home_frame.pack_forget()
page_a_frame = tk.Frame(window)
page_a_frame.pack()
tk.Label(page_a_frame, text="This is page A").pack()
back_button = tk.Button(page_a_frame, text="Back", command=show_home_page)
back_button.pack()
def show_page_b():
global page_b_frame
home_frame.pack_forget()
page_b_frame = tk.Frame(window)
page_b_frame.pack()
tk.Label(page_b_frame, text="This is page B").pack()
back_button = tk.Button(page_b_frame, text="Back", command=show_home_page)
back_button.pack()
# Define the function to show the login page
def show_login_page():
global login_frame
global home_frame
home_frame.pack_forget()
login_frame = tk.Frame(window)
login_frame.pack()
# Create the form for the user to enter their username and password
username_label = tk.Label(login_frame, text="Username:")
global username_entry
username_entry = tk.Entry(login_frame, fg="blue")
password_label = tk.Label(login_frame, text="Password:")
global password_entry
password_entry = tk.Entry(login_frame, show="*")
# Place the form widgets on the login page
username_label.pack()
username_entry.pack()
password_label.pack()
password_entry.pack()
# Create the submit button
submit_button = tk.Button(login_frame, text="Submit", command=check_login)
submit_button.pack()
# Show the login page
show_login_page()
# Run the main loop
window.mainloop()
I never used tkinter before.我以前从未使用过 tkinter。
But adding the below line to top seems to be working.但是将下面的行添加到顶部似乎有效。
home_frame = tk.Frame(window)
at line number 10.在第 10 行。
EDIT: More info - my understanding编辑:更多信息-我的理解
In my understanding, you are setting the home_frame
variable in only show_home_page
function.据我了解,您仅在show_home_page
function 中设置home_frame
变量。
But, you're calling home_frame.pack_forget
, before show_home_page
function is called.但是,您在调用home_frame.pack_forget
之前调用show_home_page
。
so, home_frame is actually nothing.所以,home_frame 实际上什么都不是。
just giving global variable will not assign it anything, see my test below只给全局变量不会分配任何东西,请参阅下面的测试
def foo(a):
global c
print(a)
len(c)
print(c)
>>> foo('adfa')
adfa
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in foo
NameError: name 'c' is not defined
if you assign something to c at the top, as we did in your case, it will consider that as something, and you will not get not defined error
.如果您像我们在您的情况下所做的那样,在顶部将某些内容分配给 c,它会将其视为某些内容,并且您不会收到not defined error
。
If you notice carefully, you will see that you are calling the function show_login_page()
first, and you are trying to use the variable home_frame
from the global space which is not really declared already.如果您仔细观察,您会发现您首先调用了 function show_login_page()
,并且您正在尝试使用尚未真正声明的全局空间中的变量home_frame
。
You are first decalring and assigning home_frame
in the show_home_page()
function, but that's been called after the show_login_page()
, so there is no way for show_login_page()
to know about home_frame
.您首先在show_home_page()
function 中贴花和分配home_frame
,但在show_login_page()
之后被调用,因此show_login_page()
无法了解home_frame
。
So to fix this issue, you will have to declare and assign home_frame
in the global space below the line where you assigned your login_frame
variable.因此,要解决此问题,您必须在分配login_frame
变量的行下方的全局空间中声明并分配home_frame
。
So, the final code will be something like this:所以,最终的代码将是这样的:
import tkinter as tk
# Create the main window
window = tk.Tk()
window.title("Login")
window.geometry("300x300")
# Create the login page as a frame
login_frame = tk.Frame(window)
home_frame = tk.Frame(window)
def check_login():
# Get the username and password from the form
username = username_entry.get()
password = password_entry.get()
# Check the login credentials
if username == "1" and password == "1":
# If the login is successful, destroy the login page and show the home page
login_frame.pack_forget()
show_home_page()
print("Yo")
else:
# If the login is unsuccessful, show an error message
error_label = tk.Label(login_frame, text="Invalid login. Please try again.")
error_label.pack()
# Create the submit button
submit_button = tk.Button(login_frame, text="Submit", command=check_login)
submit_button.pack()
# Define the function to show the home page
def show_home_page():
# Create the home page as a frame
home_frame.pack()
try:
page_a_frame.pack_forget()
page_b_frame.pack_forget()
login_frame.pack_forget()
except NameError:
pass
# Create the buttons to navigate to the other pages
logout_button = tk.Button(home_frame, text="Logout", command=show_login_page)
page_a_button = tk.Button(home_frame, text="Page A", command=show_page_a)
page_b_button = tk.Button(home_frame, text="Page B", command=show_page_b)
logout_button.pack()
page_a_button.pack()
page_b_button.pack()
# Define the functions to show the other pages
def show_page_a():
global page_a_frame
home_frame.pack_forget()
page_a_frame = tk.Frame(window)
page_a_frame.pack()
tk.Label(page_a_frame, text="This is page A").pack()
back_button = tk.Button(page_a_frame, text="Back", command=show_home_page)
back_button.pack()
def show_page_b():
global page_b_frame
home_frame.pack_forget()
page_b_frame = tk.Frame(window)
page_b_frame.pack()
tk.Label(page_b_frame, text="This is page B").pack()
back_button = tk.Button(page_b_frame, text="Back", command=show_home_page)
back_button.pack()
# Define the function to show the login page
def show_login_page():
global login_frame
global home_frame
home_frame.pack_forget()
login_frame = tk.Frame(window)
login_frame.pack()
# Create the form for the user to enter their username and password
username_label = tk.Label(login_frame, text="Username:")
global username_entry
username_entry = tk.Entry(login_frame, fg="blue")
password_label = tk.Label(login_frame, text="Password:")
global password_entry
password_entry = tk.Entry(login_frame, show="*")
# Place the form widgets on the login page
username_label.pack()
username_entry.pack()
password_label.pack()
password_entry.pack()
# Create the submit button
submit_button = tk.Button(login_frame, text="Submit", command=check_login)
submit_button.pack()
# Show the login page
show_login_page()
# Run the main loop
window.mainloop()
I ran this program.. It works, but it still have some layout bugs which you have to look out for.我运行了这个程序..它可以工作,但它仍然有一些你必须注意的布局错误。 Good luck fixing that:)祝你好运:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.