Home > OS >  TKinter app - not showing frames in oop approach
TKinter app - not showing frames in oop approach

Time:12-02

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()

CodePudding user response:

You should also manage the geometry of your MainFrame inside the App, for example by packing it:

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.pack(fill='both', expand=True)  # PACKING FRAME
        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()
  • Related