Tkinter: one big class for the whole application
Hello StackOverflow Community,
I am the first GUI coder looking for advice. I'm doing my Master of Science in Physical Chemistry, for context.
I have a simple question: Is it considered bad practice to completely tie my code to a single class? I tried to split my code into classes, but I can't seem to get the magic initialization method when working with multiple classes. For reference, I've added my own init . Maybe you could help me understand how I could separate the whole thing into different classes that could go into separate modules.
Thank!
class ApplicationUI(tk.Tk):
def __init__(self):
"""
Initialises the GUI and parent.
"""
tk.Tk.__init__(self)
self.create_canvas()
self.create_menus()
self.create_main_buttons()
self.data = {}
self.call_counter = 0
self.file_opts = {}
self.file_opts['filetypes'] = [('Text Files', '.txt'),('CSV Files', '.csv'),('All Files', '.*')]
self.file_opts['initialdir'] = 'C:\\Users\jcos\Documents'
self.file_opts['title'] = 'File'
app = ApplicationUI()
app.mainloop()
source to share
As mentioned in the comments, this is a subjective question or a question of trade pros and cons.
Anyway, there is a nice pdf of clean code that discusses some approaches and rules of thumb. There is also an entire chapter (10) on classes. So maybe this is a good place to start feeling.
source to share