Python Forum
Help me understand this... (Classes and self).
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help me understand this... (Classes and self).
#11
Here are some buttons added to two different frames, 1 button is added inside the class the other is added outside of it.
import tkinter as tk

class MainFrame(tk.Frame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.pack()
        button = tk.Button(self, text='button1')
        button.pack(side="top")
        
class AnotherFrame(tk.Toplevel):
    def __init__(self, parent=None):
        super().__init__(parent)
        button = tk.Button(self, text='button3')
        button.pack(side="top")


root = tk.Tk()
main_frame = MainFrame(root)
button = tk.Button(main_frame, text='button2')
button.pack(side="top")
another_frame = AnotherFrame(main_frame)
button = tk.Button(another_frame, text='button4')
button.pack(side="top")

root.mainloop()
Reply


Messages In This Thread
RE: Help me understand this... (Classes and self). - by Yoriz - Mar-23-2019, 10:47 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand classes menator01 7 3,478 Oct-27-2019, 04:26 PM
Last Post: menator01
Question Helping understand classes Miraclefruit 10 6,536 Nov-27-2017, 01:58 PM
Last Post: Windspar
  Using classes? Can I just use classes to structure code? muteboy 5 5,236 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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