我正在尝试在tkinter中建立一个接口,但是回到菜单时遇到了一些问题。用户登录后,将显示主菜单,它具有不同的选项。选择一个选项后,它将弹出另一个窗口,该窗口具有返回菜单的选项。这就是问题所在,因为它带我进入了“登录”页面。在我将所有代码都放在一个文件中之前,它已经可以工作了,但是我决定将其拆分为不同的文件。预先感谢。
这是我的代码 登录
class Login:
def __init__(self, master):
self.master = master
self.master.title("Login")
frame = LabelFrame(master, text="Login")
frame.grid(row=0, column=0)
self.lb1 = Label(frame, text="Username").grid(row=1, column=0)
self.entry1 = Entry(frame)
self.entry1.focus()
self.entry1.grid(row=1, column=1)
self.lb2 = Label(frame, text="Password").grid(row=2, column=0)
self.entry2 = Entry(frame, show="*")
self.entry2.grid(row=2, column=1)
btn = ttk.Button(frame, style="BM.TButton", text="Login", command=self.btn_clicked)
btn.grid(row=3, columnspan=2, sticky=W + E)
master.bind('<Return>', lambda event: self.login())
def login(self):
username = self.entry1.get()
password = self.entry2.get()
if username == "Admin" and password == "12345":
print("Success")
root2 = Toplevel(self.child)
myGui = Menu(root2)
self.child.withdraw()
else:
messagebox.showerror("Error", "Invalid login")
print("Retry")
def main():
mi_app = Login
return 0
if __name__ == '__main__':
master = Tk()
app = Login(master)
master.mainloop()
菜单
class Menu:
def __init__(self, master):
self.master = master
self.master.title("Menu")
self.master.geometry("1024x640")
self.master.config(background="light blue")
self.labelTitle = Label(master, text="Menu",
).grid(row=0)
self.button1 = Button(master, text="Inventory", width=12,
height=5, command=self.btn,
).grid(row=2, column=0)
def btn(self):
root2 = Toplevel(self.master)
myGui = Window1(root2)
self.master.withdraw()
Window1
class Window1:
def __init__(self, master):
self.master = master
self.master.title("Window1")
self.master.geometry("830x640")
top = Frame(self.master)
top.grid(row=0, column=0, sticky=E + W + N + S)
self.button1 = ttk.Button(top_frame, text="Menu", command=self.home) # home button
self.button1.grid(row=0, column=0)
def button1(self):
root2 = Toplevel(self.master)
myGui = Menu(root2)
self.master.withdraw()