Python Forum
[Tkinter] Inheritance
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Inheritance
#1
The following code was written primarily by Yoriz. I am trying to see how it words inorder to learn classes and Tkinter.

I know something about classes but I have a lot to learn.

StartFrame is a child of tk.Frame. Inheritance is used so that a child child class inherits the properties and methods from the parent class. If the parent class is created by the programmer, I can view its properties and methods. I can't review the properties and methods of tk.Frame since it is a built in class. Obviously the frame can not be built without StartFrame being a subclass of tk.Frame, but I have never built a frame this way.

What property or method is Subclass inheriting from tk.Frame and how is it being used?

import tkinter as tk


class Main:
    def __init__(self, root):
        self.root = root
        self.root.geometry("800x800")
        self.create_start_frame()

    def create_start_frame(self):
        self.start_frame = StartFrame(self.root)
        self.start_frame.btn_start.bind(
            '<Button-1>', self.on_start_frame_btn_start)
        self.start_frame.pack()

    def on_start_frame_btn_start(self, event):
        self.start_frame.destroy()
        self.second_menu_frame = SecondMenuFrame(root)
        self.second_menu_frame.btn_option1.bind(
            '<Button-1>', self.on_second_menu_frame_btn_option1)
        self.second_menu_frame.pack()

    def on_second_menu_frame_btn_option1(self, event):
        self.second_menu_frame.destroy()
        self.final_frame = FinalFrame(root)
        self.final_frame.pack()


class StartFrame(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        tk.Label(self, text='StartFrame').pack()
        self.btn_start = tk.Button(
            self, text="Press here to start", bg="red", fg="black", font="Helvitica 30")
        self.btn_start.pack()


class SecondMenuFrame(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        tk.Label(self, text='SecondMenuFrame').pack()
        self.btn_option1 = tk.Button(self, text="Option1", bg="white",
                                     fg="firebrick", relief="groove", font="Helvitica 30")
        self.btn_option1.pack()


class FinalFrame(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        tk.Label(self, text='FinalFrame').pack()
        self.btn_final1 = tk.Button(self, text="finalsel1", bg="white",
                                    fg="firebrick", relief="groove", font="Helvitica 30", )
        self.btn_final1.pack()

print(issubclass(FinalFrame, Main))

if __name__ == "__main__":
    root = tk.Tk()

    main = Main(root)
    root.mainloop()
Reply
#2
Startframe is subclassed from Frame. Parent/child is a different relationship. Replace parent with superclass and child with subclass in your description.

You can see all the attributes of any class, built-ins, downloaded or your own modules. In many cases you can even see the code. From the Python shell:
Output:
>>> import inspect >>> print(inspect.getsource(tkinter.Frame.pack_forget)) def pack_forget(self): """Unmap this widget and do not use it for the packing order.""" self.tk.call('pack', 'forget', self._w)
Not very interesting since all the fun stuff is done in "C".

I think the main thing you are getting from frame is the ability to add child widgets. Frame/Button is a parent/child relationship. Not class inheritannce, but the parent Frame object contains the child Button object
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020