5

I have written multiple gui applications using python, but each one is a mess of initializations and functions and I don't feel like I'm using the object oriented capabilities to their full potential. I have searched the net, but haven't found any place that shows the best practices for this kind of program. So for a complex gui application with multiple labels, menus, buttons and screens, along with some back end computation, what is the best way to structure the program?

Currently, I'm just doing all initializations of frames, labels, etc in the constructor of the class and calling a function to loop and do the back end stuff.

saladboy97
  • 51
  • 3
  • have you seen [Best way to structure a tkinter application](http://stackoverflow.com/a/17470842/7432)? – Bryan Oakley Jul 05 '18 at 02:43
  • I have, and I have been able to do it for smaller programs with less elements. But in programs with a lot of back end, multiple files, windows, labels, etc, I start to lose track of things. So, what I wanted to know is how to modularize the code even further. – saladboy97 Jul 05 '18 at 02:53

1 Answers1

0

I'm not an expert, take what i say as a personal recommendation only from 4 years working on the same Tkinter project.

My first piece of advice would be to segment everything to the bare minimum of what it can be and create function to do those specific tasks. If you require 4 user input widgets for a form then those should be built inside their own function.

Create a class to handle background processing in a separate thread and use the after() method to check if it has been completed to avoid freezing the gui by processing on the main thread.

If you have large parts of your GUI that is interlinked then make a new class for that in a seperate py file and import it to fully segment it from your main code. This forces you to modularize your code so sections are not stacked on top of each other causing a single error to brake multiple parts. As an example i made a calendar wich has the days of the month by week as buttons and also an event input form which i moved to a new py file and just used mycal = moduals.create_calendar(root, master, **kwargs)

Tkinter is grate for sub-classing. use that to your advantage as much as possible. I have been working on a Tkinter wrapper for a while now which has a lot of good examples on subclassing Tkinter widgets:

PyPi rapidTk

GitHub

Scott Paterson
  • 392
  • 2
  • 17