How to inherit a tkinter GUI window with OOP class? How to inherit a tkinter GUI window with OOP class? tkinter tkinter

How to inherit a tkinter GUI window with OOP class?


You shouldn't be inheriting from the "Home" class at all. If you want the buttons to be available for every page on the right, they shouldn't be part of the pages. If you have them in a class and have every page inherit from that class, you'll end up with 25 buttons instead of 5.

Instead, divide your root window into two: a frame on the left for the buttons, a frame on the right for each individual window. You can then either create the buttons in a separate class or separate function. At the end, the class for Home, Overview, etc should all be identical and none of them should have the buttons.


You should split the commands below into a function. Now after changing a screen you can just call it in new function self.left_menu().

    def left_menu(self):        # Menu items        self.home = tk.Button(menu, text='Home')        self.overview = tk.Button(menu, text='Overview')        self.apartments = tk.Button(menu, text='Apartments')        self.students = tk.Button(menu, text='Students')        self.stats = tk.Button(menu, text='Statistics')        # Menu items location        self.home.grid(row=0, column=0, pady=(25, 50), padx=15)        self.overview.grid(row=1, column=0, pady=(0, 50), padx=15)        self.apartments.grid(row=2, column=0, pady=(0, 50), padx=15)        self.students.grid(row=3, column=0, pady=(0, 50), padx=15)        self.stats.grid(row=4, column=0, pady=(0, 50), padx=15)

If you will be calling it outside of a class you can do that by assigning the class to a variable and then calling the function.

obj = Home()obj.left_menu()