Something must have gone wrong in my TKinter project when I restructured the code to conform to the OOP paradigm.
The MainFrame is no longer displayed. I would expect a red frame after running the code below, but it just shows a blank window.
import tkinter as tk
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("App")
self.geometry("800x600")
main_frame = MainFrame(self)
main_frame.tkraise()
class MainFrame(ttk.Frame):
def __init__(self, container):
super().__init__(container)
s = ttk.Style()
s.configure("top_frame.TFrame", background="red")
self.my_frame = ttk.Frame(self, style="top_frame.TFrame")
self.my_frame.pack(fill="both", expand=True)
if __name__ == "__main__":
app = App()
app.mainloop()
.pack()
or any other geometry manager onmain_frame
, so there's no possibility for its child Frame to be visible.self.pack(fill="both", expand=True)
in the init-function of the MainFrame-class resolved the issue.Frame
that's a child of theMainFrame
class ispack
ed on__init__
:self.my_frame.pack(fill="both", expand=True)
, but sinceMainFrame
inherits fromttk.Frame
itself, I think you're probably right. I'm not sure what OP gains from this inheritence, however.my_frame
is packed, but every ancestor widget all the way back to the root window also has to be packed (or otherwise managed) for that to make any difference.