如何在一个函数下全部导入 [英] How to import all under a function
问题描述
我有一个登录屏幕,登录后将进入带两个按钮的页面.食堂页面按钮和管理页面按钮.
I have a login screen that once logged in will take me to a page that has two buttons. Canteen page button, and admin page button.
我的第一个问题是,一旦我按按钮打开食堂页面,该页面内的所有内容似乎都无法正常工作.我无法从模块导入所有内容,但我必须从Canteen导入*". 我的第二个问题与第一个问题相同,只是在管理"屏幕上.
my first problem is that once I open my canteen page by pressing the button everything that's inside this page doesn't seem to be working. I assume I have to 'import * from Canteen' although I cant import all under a module. my second problem is the same as the first just with my Admin screen.
def adminpage():
import Admin
def canteenpage():
import Canteen
find_user = ('SELECT * FROM user WHERE username = ? and password = ?')
c.execute(find_user,[(self.username.get()),(self.password.get())])
result = c.fetchall()
if result:
root=Tk()
root.geometry("400x400")
root.title("Select Page")
Label(text = "welcome to the dashboard").pack()
Button(root, text = "Canteen Page",command=canteenpage).pack()
Button(root, text = "Admin Page",command=adminpage).pack()
推荐答案
您应将所有代码放在另一个模块中(在您的情况下为"Admin"和"Canteen").那对你有用.
You should put all the code in the other module(In your case "Admin" and "Canteen") in a class. That would work for you.
就像我在此示例中所做的那样:
Like I have done in this example:
模块_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()
Module_2:
class TestClass:
import tkinter as tk
root = tk.Toplevel()
lbl = tk.Label(root, text="Test Label")
lbl.pack()
这将为您服务
这篇关于如何在一个函数下全部导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!