Python GUI with Tkinter

Newsflash: you can make GUI apps with Python out of the box.

What is Tkinter?

Standard installation of Python has “Tk Interface” (and “ttk” which is the interface to the newer themed widgets) to Tcl/Tk included. Tk is a platform-independent GUI framework developed for the Tcl scripting language. Tk allows developing cross-platform (Windows, MacOS, Linux) interfaces.

cross-platform GUI
cross-platform GUI

At first glance everything looks promising, but during development you can encounter some flaws and problems with these tools.

Typical problems

1. Layout (geometry) managers

  • pack
  • grid
  • place

You should avoid mixing them in the same master window.

2. Program structure

  • function
  • class

In many cases a Tkinter program starts as a simple script with all of the code written in-line. Early in the process of development it’s totally fine, because you simply don’t have a clear understanding of the product yet.

Starting to use classes too early can introduce a lot of unnecessary boilerplate. But there is one big advantage: it helps you to avoid the use of global variables (which you will use a lot!). 

3. Main window

The main window isn’t itself part of the “ttk”, so we must put a frame inside to hold all the content. This for example ensures that background colors match with themed widgets.

4. Single-threaded

All screen updates are processed only in the event loop, but Tk is single-threaded. Because of this, any calls or computations that block event handlers should be implemented as:

  • separate threads.
  • multi-processing, 
  • asynchronous I/O calls, 
  • nested event loops, 
  • broken into smaller steps.

5. Images

Out of the box, Tk includes support for GIF, PNG, and PPM/PNM images. Tkinter uses a Python image library PIL (or better yet its fork “pillow”) which supports much more types.

It is important to note that you need to keep an additional reference to the image object, so it doesn’t get garbage collected at the end of the function.

6. Bundling

In case of using PyInstaller watch out for those:

Every dependency matters

Don’t be surprised when a simple one window app takes tens of megabytes of size.

Paths

In the entry point script (usually a script outside of your Python package that simply imports your package and runs main() function) you should avoid using explicit relative imports.
Also keep in mind that the bundled app is unpacked into a temporary directory, which may be different from a working/local one.  

Conclusion

Yes, Tkinter is poorly documented, has some unexpected behaviors, and needs some workarounds. But in the end of the day it just works.

If you really don’t want to mess with that, check out some other alternatives for making GUI apps in Python: GuiProgramming.

Get Free Email Updates!

Signup now and receive an email once I publish new content.

I agree to have my personal information transfered to MailChimp ( more information )

I will never give away, trade or sell your email address. You can unsubscribe at any time.

One Comment
  1. wyclef@gmail.com'

Leave a Reply