Python Forum
[Tkinter] proper way of coding
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] proper way of coding
#4
I'm starting to like making an App class for the root window.
import tkinter as tk

class MainWindow(tk.Tk):
    """Root window class"""
    def __init__(self, *args, title="Example", **kwargs):
        super().__init__(*args, **kwargs)
        if title:
            self.title(title)
        self.label = tk.IntVar(value=0)
        tk.Label(self, textvariable=self.label).pack()
        tk.Button(self, text='Press Me', command=self.change, width=30).pack(padx=5, pady=5)
 
    def change(self):
        self.label.set(self.label.get() + 1)

def main():
    app = MainWindow()
    app.mainloop()

if __name__ == '__main__':
    main()
Or skip making a main().
if __name__ == '__main__':
    MainWindow().mainloop()
Reply


Messages In This Thread
proper way of coding - by ebooczek - Feb-20-2022, 05:50 PM
RE: proper way of coding - by Larz60+ - Feb-20-2022, 07:03 PM
RE: proper way of coding - by menator01 - Feb-20-2022, 07:06 PM
RE: proper way of coding - by deanhystad - Feb-20-2022, 10:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable pass the proper row number to Menubutton command kenwatts275 2 2,650 Jul-19-2020, 11:46 AM
Last Post: kenwatts275

Forum Jump:

User Panel Messages

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