我正在尝试运行在类LoginPage中切换帧的函数和函数buttonLoginClicked。似乎我想从CCTV init 方法运行的任何函数只能在LoginPage init 方法中工作,我不知道为什么。任何帮助将受到高度赞赏。
import tkinter as tk
from tkinter import ttk, messagebox
from Login import *
TITLE_FONT = ("Verdana", 12)
NORMAL_FONT = ("Verdana", 8)
class CCTV(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.iconbitmap(self, default="FittleworthLogo.ico")
tk.Tk.wm_title(self, "Fittleworth CCTV")
container = tk.Frame(self)
container.pack()
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (LoginPage, PageOne):
frame = F(container, self)
self.frames[F] = frame
frame.grid(column=0, row=0, sticky="nsew")
self.show_frame(LoginPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class LoginPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.labelTitle = ttk.Label(self, text="Login Page", font=TITLE_FONT)
self.labelUsername = ttk.Label(self, text="Username", font=NORMAL_FONT)
self.entryUsername = ttk.Entry(self, font=NORMAL_FONT)
self.labelPassword = ttk.Label(self, text="Password", font=NORMAL_FONT)
self.entryPassword = ttk.Entry(self, show = "*", font=NORMAL_FONT)
self.buttonLogin = ttk.Button(self, text="Login", command=self.buttonLoginClicked)
#self.buttonLogin = ttk.Button(self, text="Login", command=lambda: controller.show_frame(PageOne))
self.labelTitle.grid(row=0, columnspan=2, pady=10)
self.labelUsername.grid(row=1, column=0, sticky="w")
self.entryUsername.grid(row=1, column=1)
self.labelPassword.grid(row=2, column=0, sticky="w")
self.entryPassword.grid(row=2, column=1)
self.buttonLogin.grid(row=3, columnspan=2, pady=10)
def buttonLoginClicked(self):
global account
username = self.entryUsername.get()
password = self.entryPassword.get()
validPassword = loggingIn(account, username, password)
#This isn't working either
self.buttonTest = ttk.Button(text="TEST", command=lambda: controller.show_frame(PageOne))
self.buttonTest.grid(row=4)
if validPassword:
#Need to switch frames to PageOne here
#This doens't work
controller.show_frame(PageOne)
#This does
tk.messagebox.showinfo("Login successful", "You are now logged in")
else:
tk.messagebox.showinfo("Login error", "Incorrect username or password")
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.labelTitle = ttk.Label(self, text="Login Page", font=TITLE_FONT)
self.loginButton = ttk.Button(self, text="Login", command=lambda: controller.show_frame(LoginPage))
self.labelTitle.grid(row=0, columnspan=2, pady=10)
self.loginButton.grid(row=3, columnspan=2, pady=10)
app = CCTV()
app.geometry("800x600")
account = gettingAccountInformation()
app.mainloop()
答案 0 :(得分:1)
我用以下内容自行解决..
在每个类的 init 方法中添加:self.controller = controller
然后调用它:self.controller.show_frame(Page)
无论如何这对我有用!