0

First of all my english is not the best so excuse me for writing bad. I am new to programming oop with tkinter and I looked up 3 methods to initialise the main program. My question with this what are the difference between these and are there any advantages or is this just personal preference?

The first Method is to create the Frame in if block

from tkinter import *
from tkinter import ttk

class ConverterFrame(Frame):
    def __init__(self, container):
        super().__init__(container)
        self.grid()
        # Create Frame

class App(Tk):
    def __init__(self):
        super().__init__()
        #Set options for main window

if __name__ == "__main__":
    app = App()
    ConverterFrame(app)
    app.mainloop()

The second Method creates the Frame inside the App Class in a def block

from tkinter import *
from tkinter import ttk

class ConverterFrame(Frame):
    def __init__(self, container):
        super().__init__(container)
        # Create Frame

class App(Tk):
    def __init__(self):
        super().__init__()
        self.createwidgets()

    def createwidgets(self):
        frame1 = ConverterFrame
        frame1.grid()

if __name__ == "__main__":
    app = App()
    app.mainloop()

and then there is this

from tkinter import *
from tkinter import ttk

def main():
    program = CounterProgram()
    program.mainloop()

class CounterProgram:

    def __init__(self):
        #Programm

if __name__ == "__main__":
    main()

I hope it is understandable what I mean.

quamrana
  • 33,740
  • 12
  • 54
  • 68
  • For the first method, it's not advisable to call `self.grid()`. Instead you should leave the creator of the `ConverterFrame` object to call `.grid()`. For an example look [here](https://stackoverflow.com/a/30489525/11106801). That is so that people can import and use your widget without being limited to only using `.grid` in that container. The 3rd approach is the least reusable. It wouldn't be likely that someone can use your class for anything since you locked it and it's hard to add new widgets. But between the first 2 methods it's just about personal preference. – TheLizzard Jun 30 '21 at 08:47

0 Answers0