The first thing to do when trying out a new GUI toolkit is to get some visuals on the screen and play around with some minimal code to get a good base for further improvement. In this first part of the ttk tutorial I describe various ways to create the bare minimum window with the python tkinter.ttk module as an introduction to the series.
Requirements: Python 3.1+ interpreter compiled with tkinter
Since version 3.1 python adds the tkinter.ttk module which extends tkinter and uses the Tk 8.5 widget framework improvements including themes, anti-aliased fonts, transparency and new widgets. These improvements make the python standard library GUI part somewhat usable and much less ugly than before. It also eases portable GUI development and deployment. The main problem I see with it so far is the practically non existent documentation. Being a good hacker I scratch this itch of mine and write a tutorial series while exploring this topic.
Example 1: Clean and documented minimal application
This first example shows how to crate a minimal application with the new ttk widget set. The only thing this application does is create a root window and enter the main loop. The visual result looks something like this:
Code for the first example (gist) ttk010.py:
Example 2: More minimised version#! /usr/bin/env python3
Minimal ttk application that only displays an empty window and runs the main loop.
What's it doing:
1. Create root frame
2. Run application main loop
from tkinter import ttk
frame = ttk.Frame()
Here is a second example that does the same thing without comments and execution parameters. You'll have to execute it with the interpreter. Something like "$ python3 ttk011.py".
Code for the second example ttk011.py:
from tkinter import ttk
Example 3: Even more minimised version
Another, even smaller variation of the code -- to give the title of the article meaning and play around a bit -- is a one liner. I use the inbuilt __import__ function for it and it makes the code quite ugly too, so it is mainly of educational interest.
Code for the third example ttk012.py
Example 4: System shortcut
To show another perspective, the final example aims for the minimal hassle design target. This example can simply be run in a terminal:
Code for the last example ttk013.py
echo "__import__('tkinter', fromlist=['ttk']).Frame().mainloop()" | python3
The purpose of this post was to create a kick off point for ttk development and play around with the basics to make you comfortable with the framework. In the next post of this series I will do some structural organisation on the code to create something maintainable in the long run and add some more widgets.
Tkinter+Ttk Tutorial Part 2: Menu, label and callbacks.