Python Desk Top Applications

From OpenCircuits
Jump to: navigation, search

This will be a page about how I write my desktop applications, and so naturally, how I would recommend they be writtern. It also documents the structure of most of the applications that I have documented on this wiki. Try them and let me know how you think they might be better.

Python Smart Plug Technical



General Information

These notes are here so you can more easily modify my code and build your own.

Here is an overview of the general plan, details can be filled out by reading the example code in my projects ( see category ..... below).

The overall architecture is called the model view controller or MVC.

The main program is the controller.

The view component is called GUI ( typically with the class name GUI ). It creates all the visible components, and relays user input to the controller. You can unplug the GUI object from the application and plug in new components. Don't like the GUI? You could modify mine, or you could make a modification and choose which one to use. This is sort of like a skin for an application.

Other Important Components


Logger ( in ) and Parameters ( in ). The controller creates one of each, and make them available to the other components. The other components can interact with them, and uses them respectively for logging events, and getting access to parameters ( those aspects of the application that are particularly easy to change ). I describe more of this in My Python Coding Conventions


This is a fairly big topic. It is how you or your user easily controls some aspect of the program. See Configuration Files For Python.


Global Values: Yes I know that globals are bad, but they can be useful. For example many class instances need to access the parameter file. This can be done using the singleton class AppGlobal. It has values at the class level ( not instance ) that are available simply by importing the class. Many values are originally defaulted to None, and are set to valid values as the application initializes and runs, starting, default values often come from parameters.

Helper Tread

The application has a main thread running in a Tkinter mainloop. There is also a second thread called a "helper" running which makes some processing much easier. To make GUI mainloop responsive to both the GUI and its own processing it uses a pseudo event loop or a polling subroutine that is implemented in xxxxx. The frequency which polling occurs is set in parameters, the relatively low rate of 100 ms between calls ( .1 sec ) seems to give a perfectly responsive application in most cases. I have run it as fast as once every 10 ms. Have not tried to find a limit.


Development Environment

  • Windows 10 64bit
  • Anaconda Spyder 3.x
  • Python 3.7

Runtime Environments

Should Run in:

  • Windows, Mac, Linux, Raspberry Pi with desktop support ( not headless )
  • Python 3.6 up.

I have run in: Windows 10, Python 3.7

  • more coming, or let me know.

Components and Functions

  • Tkinter for the GUI
  • pyLogging for logging
  • SmartTerminal a model-view-controller controller with some modifications ( Model–view–controller - Wikipedia or perhaps closer to Model–view–presenter - Wikipedia )
  • Second thread for processing without being blocked by/blocking the GUI
  • Global Singleton for app cohesion.
  • Parameter file for easy customization.
  • Mathplotlib for graphing.
  • SQLLite for the database.

The Controllers for Smart Plug

The class for the MVC controller is also the class you create to run the application: see the code at the bottom of the file, just run the file. Similar code is at the bottom of some of the other source files to make it convenient to run from those files, this is just to make it easier in development, the code in is the model for what should be used. Sometimes the code at the bottom of other file may have code for testing objects in the file, it may not be maintained, may not work as intended.

The __init__ method is the initialization and "run" method for the application. Much of the code that would normally be in it has been moved to .restart which is used to restart the application when just the parameters have been changed. See the docstring there.


The application runs two threads, one for the GUI and one where processing can occur ( HelperThread ) with out locking up the GUI. There are 2 queues that allow the threads to communicate. Multithreading is not used in the graphing application as of now.

The Tkinker Thread

Once Tkinker is started it runs its own mainloop. In order to receive however we need to check the rs232 port from time to time. This is done in SmartTerminal.polling() I call this thread the GUI thread or gt. The terminal is configured to have a second thread called the Helper Thread or ht. In some cases the receiving of data is passed to the Helper Thread so where data is processed in addition to being posted to the GUI. See the section HelperThread for more info.


Refers to similar but different program !! Will update for this program soon.

HelperThread in This class provides the support for a second thread of execution that does not block the main thread being run by Tinker. I call the two threads the GUI Thread (gt) and the Helper Thread ( ht ). It can get confusing keeping track of which method is running in which thread, I sometimes annotate them with gt and ht. The helper thread is started by running which pretty much just runs a polling task in HelperThread.polling(). HelperThread.polling() is an infinite loop, it uses sleep to set the polling rate. When used with the green house processing module, it may call a function there that is its own infinite loop. There are a lot of details here, I should write some more about it.


Parameters ( in ) this is pretty much a structure ( that is all instance variables ) that is instantiated early in the life of the application. It passes values, strings, numbers, objects to application elements that need them. The instance of parameters is made globally available thru the AppGlobal class. Values in Parameters are treated as constants, read only. Much of the appearance and behavior of the application is controlled here.

The standard gui has a button to kick off editing of this file, the application may then be restarted ( another button ) with the new values.

There are a couple of meta parameters, including os_win, mode and computername which then may be used in conditionals later in Except for this sort of thing there is really not much "code" in parameters. You can change this code pretty much as much as you like, as long as you end up setting up values for the required parameters.

The code is extensively commented: use that for documentation.

Values are often set to None as a default, then later set to some other value. Or the value may be set several times in a row ( this is an artifact of messing with the values ); only the last value set has any meaning.

For more info see: Configuration Files For Python


The database is SQLLite. So far it seems adequate to the task. Access is via string of SQL and variables bound to those statements. Look for the code in .......


Both threads have method that perform polling for events often for items in their queue that may have been sent from the other thread. Info on a similar app in Python Smart Terminal Technical Details.


This uses the standard Python logging class. Logging level and other logging details are controlled using the parameter file.

Other Classes

For now the documentation, as far as it exists is in the source code. This probably will not change.

Coding Conventions

See: My Python Coding Conventions

See Also

Look at the categories below, especially the one Python Projects Python Control of Smart Plugs

Personal tools