[Tkinter] Inheritance - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [Tkinter] Inheritance (/thread-32514.html) |
Inheritance - Heyjoe - Feb-14-2021 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() RE: Inheritance - deanhystad - Feb-15-2021 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: 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 |