我是 python 新手。我正在嘗試創建登錄頁面。單擊登錄按鈕后,我需要更改框架。這是錯誤
Tkinter 回呼 Traceback 中的例外(最近一次呼叫最后一次):檔案“C:\Users\Rashith Senishka\AppData\Local\Programs\Python\Python39\lib\tkinter_ init _.py”,第 1892 行,呼叫中 回傳 self.func (*args) 檔案“D:\Python\tkinter\Test_2\Proxlight_Designer_Export\main.py”,第 57 行,在 self.login_button = tk.Button(self, command=lambda: master.show_frame()) AttributeError: 'Frame ' 物件沒有屬性 'show_frame'
請幫我解決這個錯誤
import tkinter as tk
class SchoolManegmentSystem(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
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 (LoginPageWidget, LoginPageWidget_W):
frame = F(container)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(LoginPageWidget)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class LoginPageWidget(tk.Frame):
def __init__(self, master=None, **kw):
super(LoginPageWidget, self).__init__(master, **kw)
self.backg = tk.Label(self)
self.img_background = tk.PhotoImage(file='background.png')
background_color = "#1f1f1f"
self.backg.configure(background=background_color, cursor='arrow', font='TkDefaultFont',
image=self.img_background)
self.backg.configure(relief='flat')
self.backg.place(anchor='nw', x='0', y='0')
self.loginlabel = tk.Label(self)
self.loginlabel.configure(anchor='n', background='#e7f4f9', compound='top', font='{Poppins} 23 {}')
self.loginlabel.configure(foreground='#4f545c', justify='center', relief='flat', text='LOGIN')
self.loginlabel.place(anchor='nw', x='460', y='70')
self.usernamelabel = tk.Label(self)
self.usernamelabel.configure(background='#e7f4f9', font='{Poppins} 12 {}', foreground='#828282', relief='flat')
self.usernamelabel.configure(text='USERNAME')
self.usernamelabel.place(anchor='nw', x='380', y='160')
self.input_field_bg = tk.Label(self)
self.img_img_textBox0 = tk.PhotoImage(file='img_textBox0.png')
self.input_field_bg.configure(background='#e7f4f9', image=self.img_img_textBox0, relief='flat')
self.input_field_bg.place(anchor='nw', x='365', y='185')
self.password_label = tk.Label(self)
self.password_label.configure(background='#e7f4f9', font='{Poppins} 12 {}', foreground='#828282', relief='flat')
self.password_label.configure(text='PASSWORD')
self.password_label.place(anchor='nw', x='380', y='240')
self.input_field_bg_2 = tk.Label(self)
self.input_field_bg_2.configure(background='#e7f4f9', image=self.img_img_textBox0, relief='flat')
self.input_field_bg_2.place(anchor='nw', x='365', y='265')
self.login_button = tk.Button(self, command=lambda: master.show_frame())
self.img_img0 = tk.PhotoImage(file='img0.png')
self.login_button.configure(activebackground='#e7f4f9', activeforeground='#e7f4f9', background='#e7f4f9',
borderwidth='0')
self.login_button.configure(cursor='hand2', disabledforeground='#e7f4f9', foreground='#e7f4f9',
highlightbackground='#e7f4f9')
self.login_button.configure(highlightcolor='#e7f4f9', image=self.img_img0, relief='flat')
self.login_button.place(anchor='nw', x='400', y='350')
self.username_input_field = tk.Entry(self)
self.username_input_field.configure(background='#C7E4E0', font='{Poppins} 12 {}', insertbackground='#e7f4f9',
insertborderwidth='3')
self.username_input_field.configure(insertwidth='2', relief='flat', show='?')
self.username_input_field.place(anchor='nw', height='40', x='380', y='269')
self.password_input_field = tk.Entry(self)
self.password_input_field.configure(background='#C7E4E0', font='{Poppins} 12 {}', insertbackground='#e7f4f9',
insertborderwidth='3')
self.password_input_field.configure(insertwidth='2', relief='flat')
self.password_input_field.place(anchor='nw', height='40', x='380', y='190')
self.button1 = tk.Button(self)
self.button1.configure(relief='flat', text='CLEAR')
self.button1.place(anchor='nw', x='0', y='0')
self.configure(borderwidth='0', height='515', relief='flat', width='791')
self.place(anchor='nw', x='0', y='0')
class LoginPageWidget_W(tk.Frame):
def __init__(self, master=None, **kw):
super(LoginPageWidget_W, self).__init__(master, **kw)
self.backg = tk.Label(self)
self.img_background = tk.PhotoImage(file='background.png')
background_color = "#ffffff"
self.backg.configure(background=background_color, cursor='arrow', font='TkDefaultFont',
image=self.img_background)
self.backg.configure(relief='flat')
self.backg.place(anchor='nw', x='0', y='0')
self.loginlabel = tk.Label(self)
self.loginlabel.configure(anchor='n', background='#e7f4f9', compound='top', font='{Poppins} 23 {}')
self.loginlabel.configure(foreground='#4f545c', justify='center', relief='flat', text='LOGIN')
self.loginlabel.place(anchor='nw', x='460', y='70')
self.usernamelabel = tk.Label(self)
self.usernamelabel.configure(background='#e7f4f9', font='{Poppins} 12 {}', foreground='#828282', relief='flat')
self.usernamelabel.configure(text='USERNAME')
self.usernamelabel.place(anchor='nw', x='380', y='160')
self.input_field_bg = tk.Label(self)
self.img_img_textBox0 = tk.PhotoImage(file='img_textBox0.png')
self.input_field_bg.configure(background='#e7f4f9', image=self.img_img_textBox0, relief='flat')
self.input_field_bg.place(anchor='nw', x='365', y='185')
self.password_label = tk.Label(self)
self.password_label.configure(background='#e7f4f9', font='{Poppins} 12 {}', foreground='#828282', relief='flat')
self.password_label.configure(text='PASSWORD')
self.password_label.place(anchor='nw', x='380', y='240')
self.input_field_bg_2 = tk.Label(self)
self.input_field_bg_2.configure(background='#e7f4f9', image=self.img_img_textBox0, relief='flat')
self.input_field_bg_2.place(anchor='nw', x='365', y='265')
self.login_button = tk.Button(self)
self.img_img0 = tk.PhotoImage(file='img0.png')
self.login_button.configure(activebackground='#e7f4f9', activeforeground='#e7f4f9', background='#e7f4f9',
borderwidth='0')
self.login_button.configure(cursor='hand2', disabledforeground='#e7f4f9', foreground='#e7f4f9',
highlightbackground='#e7f4f9')
self.login_button.configure(highlightcolor='#e7f4f9', image=self.img_img0, relief='flat', command=self.quit)
self.login_button.place(anchor='nw', x='400', y='350')
self.username_input_field = tk.Entry(self)
self.username_input_field.configure(background='#C7E4E0', font='{Poppins} 12 {}', insertbackground='#e7f4f9',
insertborderwidth='3')
self.username_input_field.configure(insertwidth='2', relief='flat', show='?')
self.username_input_field.place(anchor='nw', height='40', x='380', y='269')
self.password_input_field = tk.Entry(self)
self.password_input_field.configure(background='#C7E4E0', font='{Poppins} 12 {}', insertbackground='#e7f4f9',
insertborderwidth='3')
self.password_input_field.configure(insertwidth='2', relief='flat')
self.password_input_field.place(anchor='nw', height='40', x='380', y='190')
self.button1 = tk.Button(self)
self.button1.configure(relief='flat', text='CLEAR')
self.button1.place(anchor='nw', x='0', y='0')
self.configure(borderwidth='0', height='515', relief='flat', width='791')
self.place(anchor='nw', x='0', y='0')
app = SchoolManegmentSystem()
app.resizable(False, False)
app.eval('tk::PlaceWindow . center')
app.overrideredirect(True)
app.mainloop()
uj5u.com熱心網友回復:
masterofLoginPageWidget是一個框架 ( container) 里面SchoolManegmentSystem。您還需要傳遞 to 的實體SchoolManegmentSystem才能LoginPageWidget呼叫它show_frame():
class SchoolManegmentSystem(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
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 (LoginPageWidget, LoginPageWidget_W):
frame = F(container, self) # pass itself to frame as well
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(LoginPageWidget)
...
class LoginPageWidget(tk.Frame):
def __init__(self, master, controller, **kw): # added controller argument
super(LoginPageWidget, self).__init__(master, **kw)
self.controller = controller # save a reference for later use
...
self.login_button = tk.Button(self, command=lambda: controller.show_frame(LoginPageWidget_W)) # use controller instead of master
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/425920.html
