这可能有点傻但我已经在这部分工作了16个小时 . 我是编程python和使用类和对象的新手 . 任何人都可以帮我解决这段代码吗?我试图从顶层窗口调用后调用login_window重新出现在主框架中 . login_window是一个单独的类,并且已在Window类中定义的show_frame函数中调用 . 我试图在signup_window中定义的提交函数中调用show_frame函数 . 我错过了什么,我无法弄明白什么?
import tkinter as tk
来自tkinter import ttk从tkinter import messagebox导入sqlite3
LARGE_FONT =('Verdana',24)
class Window(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.iconbitmap(self, default='logo.ico')
tk.Tk.wm_title(self, "Platforuma - IoT")
container = tk.Frame(self)
container.pack(side='top', fill='both', expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight =1)
self.frames={}
for F in (login_window, signup_window, application_window):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0,column=0, sticky='nsew')
self.show_frame(login_window)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class login_window(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
userName_label = ttk.Label(self, text='User Name')
userName_label.pack(pady=10)
userName_entry = ttk.Entry(self)
userName_entry.pack()
password_label = ttk.Label(self, text='Password')
password_label.pack(pady=10)
password_entry = ttk.Entry(self, text='Password', show='*')
password_entry.pack()
login_button = ttk.Button(self, text='Log-In', width=10, command=lambda: controller.show_frame(application_window))
login_button.pack()
signup_button = ttk.Button(self, text='Sign Up', width=10, command=lambda: controller.show_frame(signup_window))
signup_button.pack()
class signup_window(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.e1_var = tk.StringVar()
self.e2_var = tk.StringVar()
self.e3_var = tk.StringVar()
userName_label = ttk.Label(self, text='User Name')
userName_label.pack(pady=10)
userName_entry = ttk.Entry(self, textvariable=self.e1_var)
userName_entry.pack()
password_label = ttk.Label(self, text='Password')
password_label.pack(pady=10)
password_entry = ttk.Entry(self, text='Password', show='*', textvariable=self.e2_var)
password_entry.pack()
confirm_password_label = ttk.Label(self, text='Confirm Password')
confirm_password_label.pack(pady=10)
confirm_password_entry = ttk.Entry(self, text='Confirm Password', show='*', textvariable=self.e3_var)
confirm_password_entry.pack()
submit_button = ttk.Button(self, text='Submit', width=10, command= self.submit)
submit_button.place(x=70, y=180)
login_button = ttk.Button(self, text='Log-In', width=10, command=lambda: controller.show_frame(login_window))
login_button.place(x=180, y=200)
def submit(self):
conn = sqlite3.connect('dataentry.db')
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS dataentry(username TEXT, password TEXT, confirmpassword TEXT)')
e1_val = self.e1_var.get()
e2_val = self.e2_var.get()
e3_val = self.e3_var.get()
c.execute("INSERT INTO dataentry (username, password, confirmpassword) VALUES (?, ?, ?)",
(e1_val, e2_val, e3_val))
conn.commit()
c.close()
conn.close()
#messagebox.showinfo("Successfull","Your information is registered. Kindly procede to Login")
win = tk.Toplevel()
win.title('Registered')
message = "Your information is registered. Kindly procede to Login"
tk.Label(win, text=message).pack()
tk.Button(win, text='Login', command=lambda: Window.show_frame(Window, login_window)).pack()
#getting error in the calling the command fuction. Everything else works fine
class application_window(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
window_label = ttk.Label(self, text='Application Window', font= LARGE_FONT)
window_label.pack(pady=10)
app = Window()app.mainloop()