链接页面时,我的页面变量不起作用

2024-04-20 14:38:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有三页。登录屏幕、食堂页面和管理员页面。当我登录时,我有两个选择:或者单击按钮将我带到食堂页面,或者单击按钮将我带到管理员页面。用按钮打开这些页面是可行的,但是,这些屏幕中的变量不起作用

所有功能都是在我的食堂和管理页面中选择的。我不知道当我这样调用变量时为什么变量不起作用,但是页面本身起作用

def CallCanteen():
    from practice import All
def CallAdmin():
    from Admin import All

 if result:
            self.master.destroy()
            root=Tk()
            root.geometry("400x400")
            root.title("Canteen System")
            Label(text = "welcome to the dashboard").pack()
            Button(root, text = "Canteen Page", command=CallCanteen).pack()
            Button(root, text = "Admin Page",command=CallAdmin).pack()

是的,我试过:

def CallCanteen():
    from practice import *
def CallAdmin():
    from Admin import *

但这会造成语法错误


1条回答
网友
1楼 · 发布于 2024-04-20 14:38:09

您应该将所有代码放在类中的其他模块(在您的例子中是“Admin”和“practice”)中。那对你有用

就像我在本例中所做的:

单元1:

import tkinter as tk


def test_func():
    from Module_2 import TestClass


root = tk.Tk()

b = tk.Button(root, text="Click", command=lambda: test_func())
b.pack()

root.mainloop()

模块2:

class TestClass:
    import tkinter as tk
    examle_variable = 0
    root = tk.Tk()
    lbl = tk.Label(root, text="Test Label")
    lbl.pack()
    root.mainloop()

要访问任何变量,请在其名称之前使用class名称。 在本例中:TestClass.example_variable

这对你有用

相关问题 更多 >