I'm new to python . i'm trying to create login page . i need to change the frame after clicking login button . this is the error
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Rashith Senishka\AppData\Local\Programs\Python\Python39\lib\tkinter_init_.py", line 1892, in call return self.func(*args) File "D:\Python\tkinter\Test_2\Proxlight_Designer_Export\main.py", line 57, in self.login_button = tk.Button(self, command=lambda: master.show_frame()) AttributeError: 'Frame' object has no attribute 'show_frame'
please help me to fix this error
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()
CodePudding user response:
master
of LoginPageWidget
is a frame (container
) inside SchoolManegmentSystem
. You need to pass instance of SchoolManegmentSystem
to LoginPageWidget
as well in order to call its 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
...