简体   繁体   English

为什么会出现错误:“home_frame is not defined”,即使它已经是全局的?

[英]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.

相关问题 NameError:未定义全局名称,为什么会收到该错误? - NameError: global name is not defined, why am I getting that error? 获取全局名称未定义错误 - getting global name not defined error 不断获取未定义的全局名称错误 - Keep getting a global name not defined error 属性未定义错误,即使它是在全局 scope 上定义的 - Attribute not defined error, even though it is defined on a global scope 为什么这样,即使我将其放置在一个类中,也始终收到未定义Driver Sponsor的错误? - Why is it so that I keep getting the error that Driver sponsor is not defined, even though I placed it within a class? "为什么我收到一个错误,指出未定义 k,即使我已返回它?" - Why am I getting an error stated that k is not defined, even though i have returned it? 为什么即使使用“ global”也没有定义变量? - Why is my variable not defined even if I used “global”? 为什么它显示变量“img”没有定义,即使它是全局的? - Why does it show that the variable "img" is not defined even though it is global? 为什么会出现错误“赋值之前引用本地变量”或“未定义全局变量”的错误? - Why am I getting either error “local variable referenced before assignment” or “global variable not defined”? Str已被定义为全局变量 - Str is already defined as a global variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM