4

Good afternoon, StackOverflow community,

I am a first-time GUI coder seeking advice. I am busy with my MSc in Physical Chemistry, for context.

I have a simple question: Is it considered poor practice to wrap the entirety of my code into a single class? I have tried to split my code up in classes, but I can't seem to get the initialising magic method right when dealing with multiple classes. For reference, I have attached my own init. Maybe you could help me understand how I could possibly split all of this up into different classes, which could go into separate modules.

Thanks!

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\xxx\Documents'
        self.file_opts['title'] = 'File'

app = ApplicationUI()
app.mainloop()
tionichm
  • 163
  • 10
  • Related: http://stackoverflow.com/q/17466561/1639625 Also pretty much opinion-based. – tobias_k May 15 '17 at 10:49
  • How large and complex are those `create_canvas` etc. methods? – tobias_k May 15 '17 at 10:50
  • I think your question is too subjective. No, it's not a bad practice to wrap entire `tk` code to a single class, if you sufficient with this (_simple is better than complex_) and your class isn't just a variables bucket (_encapsulation_). But in some cases (if things started to be _complex_ or _repetative_) it's a good idea to create some classes, representing widgets (e.g. frame, toplevel) or another related objects. – CommonSense May 15 '17 at 10:59
  • Thank you for the feedback! I'll check out that discussion that was linked. – tionichm May 15 '17 at 11:27
  • @CommonSense Thank you for the reassurance. I learned a bunch of techniques in OOP in the tutorial by Bernd Klein, but I never have had a chance to use them. I felt I was doing something wrong, because of this. – tionichm May 15 '17 at 11:34
  • I think the key concept here is the balance of complexity and repetition in methods. I feel like I have it all under control again, noting the importance of these factors. Thank you all so much! – tionichm May 15 '17 at 11:36

1 Answers1

1

As mentioned in the comments this is a subjective matter or question of trading-off pros and cons.

Anyhow there is a nice pdf on clean code which discusses some approaches and rules of thumb. There is also a whole chapter (10) on classes. So maybe this is a good place to start to get a feeling.

mrk
  • 8,059
  • 3
  • 56
  • 78