Timo
Timo

Reputation: 51

TKinter app - not showing frames in oop approach

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

Upvotes: 0

Views: 73

Answers (1)

Jan Willem
Jan Willem

Reputation: 1094

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

Upvotes: 1

Related Questions