To preface this, I have this working, I was just hoping somebody could explain why the code is behaving the way it is. I'm not understanding the grid system inside of classes very well apparently.
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('400x400')
self.rowconfigure(0, uniform=True, weight=1)
self.rowconfigure(1, uniform=True, weight=1)
self.rowconfigure(2, uniform=True, weight=1)
self.header_frame = HeaderFrame(self)
self.header_frame.grid(column=0,row=0)
self.login_frame = LoginFrame(self)
self.login_frame.grid(column=0,row=1)
self.button_frame = ButtonFrame(self)
self.button_frame.grid(column=0,row=2)
class HeaderFrame(ttk.Frame):
def __init__(self, parent):
super().__init__()
self.parent = parent
self.columnconfigure(0, uniform=True, weight=1)
self.canvas = tk.Canvas(self, bg='black')
self.canvas.grid(column=0,row=0)
class loginFrame(ttk.Frame):
def __init__(self, parent):
super().__init__()
self.parent=parent
self.columnconfigure(0, uniform=True, weight=1)
self.entryBox = ttk.Entry(self, width=33)
self.entryBox.grid(column0,row1)
class ButtonFrame(ttk.Frame):
def __init__(self, parent):
super().__init__()
self.parent=parent
self.columnconfigure(0, uniform=True, weight=1)
self.btn = ttk.Button(self, text='test')
self.btn.grid(column=0,row=2)
if __name__ == '__main__':
App().mainloop()
Now my question comes down to the grid placement. When I call each class to place the items, I specify different grid locations. header_frame.grid(column=0, row=0), login_frame(column=0,row=1), etc. However, the initial App class having a grid placement does not affect the layout of the gui whatsoever. I can place them all on row 0 and they will all still show up on separate rows until I change the grid placement inside of the individual class. I thought my grid inside of the class was being placed inside of cells of the App class, what am I missing here?
Thanks in advance for your help.
CodePudding user response:
Consider this code:
class HeaderFrame(ttk.Frame):
def __init__(self, parent):
super().__init__()
super().__init__()
is what actually creates the widget. You aren't passing parent
to super().__init__())
so the widget will be created as a child of the root window. You need to pass parent
like in the following example.
super().__init__(parent)
You need to make a similar change for all of your classes.