Examples

Examples are the main way that I learned about Tkinter and how to write code for building GUI’s . Unfortunately, it is hard to find examples. Grayson’s book has been an immense help over the years and more recently the pyttk-samples distributed by Guilherme Polo have helped greatly. The official Tcl documentation is pretty much limited to man pages which are of minor assistance in getting started with Tkinter. The tutorial on the Tcl Developers site only recently included Python code. Much experimentation was required before I was able to see a paned window on my screen or a scrolled directory. There are several interesting examples in the examples subdirectory.

Most of the examples have been reworked for version 4.2 to reflect the rework scheme have a GUI module and a support module for each GUI. So one may experiment with the examples changing sizes, colors, fonts, etc. in the GUI modules and they may still work if he does not regenerate support modules. At least, one can see some of the possibilities. Of course, I have created some of them like menu in poor taste just to illustrate some of the possibilities.

If you try running the examples by executing PAGE on the Tcl files be aware that generating and saving support files will replace the included support files which will become backup versions and the functioning of the applications will be lost.

Directory Browser

This example contains a scrolled treeview widget that displays ones UNIX directory tree. It is a rework of Polo’s example to include folder icons which open and close. Polo’s example is Based on bitwalk’s directory browser.

_images/dir-tree.png

Vrex

In the past I have used a number of regular expressions in Python and used to great advantage Visual Regexp in composing and testing those regular expressions and thought that rewriting the program in Python would be interesting because

  • It would illustrate important capabilities of PAGE.
  • It would be a useful program in its own right. Indeed, I use it all the time.
  • The original version was written in Tcl/Tk and so uses the regular expression processing of Tcl which is bound to be slightly different from that in Python. So an all Python version using the Python regular expression processing would be true to Python.
  • The original program had ZERO documentation. I never could figure out all of the facilities present. (The most recent version has one page of documentation.) I wanted a version that I could document.
  • This example illustrates several points about PAGE support: Building menus, using paned windows, text colorization, synchronization between two scrolled text windows, and a project utilizing two windows.
_images/Vrex_for_Python.png

The main portion of the GUI is a three pane window for entering the regular expression and sample in the first two and the third pane presents match results based on the selection of buttons at the bottom.

Some of the features of PAGE illustrated in this useful program are:

  • Scrolled text widgets nested inside of label frames.
  • Use of two separate GUI’s, one for the main function of the program and one for a help window.
  • Colorizing the text within the scrolled text windows.

Operation

Briefly, Vrex works as follows:

One enters the regular expression under test into the top text box, and the sample which is the subject of the regular expression match into the middle text box. One may use the File menu to load files into the text boxes, one may directly type entries or use the normal cut and paste facilities of the operating system. The user can also save the regular expression and sample using the File menu.

  • Pressing the Go button causes the match to be attempted. Also, colorization is applied to both the regular expression and to the sample. The regular expression is colorized to show the portions of the regular expression are to extracted individually. And if the match is successful the corresponding portions of the sample are displayed in the same colors.
  • By selecting one of the row of buttons marked match, 1, 2, ..., 9, the portions of the sample corresponding to extracted subpatterns are displayed in the match window.
  • You can use the File menu to load to save both the regular expression and the sample.
  • The Quit Button terminates the program.
  • The File menu also has a Quit entry that terminates the program.
  • The Help menu opens a second GUI containing a ScrolledText widget which has help information.

The regular expression should be colorized showing the portions to be extracted in different colors and as well as the matching portion of the sample.

If you then Select Match, that matching portion of the sample will be shown in the Match text box. Select 1, and the portion matching the sub expression will be shown, etc..

You can also load a file into the sample window and thus run the regular expression against many strings.

Progress Bar

This example shows the usage of the progress bar widget but is included mainly for illustrating an application with two top level windows. It shows how the main window is able to interact with variables and procedures in the imported module which actually displays the progress bar.

Two versions of this example are included, the first in the examples/progress_bar is the version that I wrote before implementing the rework scheme and the second in examples/new_progress_bar is how I changed the first to conform with the new scheme. Important features illustrated include the creation and destruction of the secondary top level window and the fact that the main GUI support code requires access to Tkinter variables defined and manipulated in the support module of the other, progress bar, window.

Complex

This really isn’t much of an example doing nothing but does showing nesting of a paned window inside of a paned window and a notebook widget nested inside of a paned window. By the way, one can enlarge the top level window and copy and paste the whole paned window to a new location.

standard.tcl

This is just a top level window with each of the standard Tk widgets that I support. It just shows that I can generate working Python code for each. The lowercase ‘s’ emphasizes that it doesn’t do much.

themed.tcl

As above for the themed widgets.

WCPE

See the WCPE directory in the examples directory. This is a program that displays the program of WCPE which is one of my favorite classical music stations. In this example all of the functional code is located in WCPE_support.py and the GUI is defined in WCPE.py which is the main module. I found it very tricky to deal with time zones and did not want to loose any of that code and have to redo it or to deal with diff’s; at the same time, I was constantly tweaking the GUI. I generated the import module once and proceeded fill out the init function and the callback functions in WCPE_support.py while tweaking the GUI, i.e., changing its location, color, the size of the display fields, and change of some display fields from entry boxes to labels.

This example requires the Python time zone package pytz which is available from Python Package Index.

_images/WCPE.png

This example utilizes:

  • Labels linked to Tkinter variables to display information like times, composer, artist, etc.
  • A Scrolled text box which displays the whole play schedule for the day and colorizes the current piece and makes sure it is displayed within the window.
  • Busy cursor to alert the user that the application is awaiting playlist information from the web.

In addition I fuss around with reading and parsing web pages and time zone calculations.

Clone

This is a somewhat kooky example located in examples/clone, where main.py creates a GUI which presents a button “Make Two” which cause the creation of two instances of the “called” GUI but with differing characteristics of location and background colors. The called GUI is then able to create incidences of clones with different locations and background colors.

It is also an example of passing a user parameter to the creation module which is then passed to the init function in the support module.

There may be a problem executing this example on Windows if fonts selected in the tcl are not available on the Windows machine. In that case the font will default to TkDefaultFont. Courier New is available on both my Linux system and my XP box.

Calendar

Small example of building a calendar display using the new rework scheme. It is located in examples/calendar subfolder.

_images/calendar.png

CPU Info

Another small example which displays the output of lscpu on a Linux system. This does not execute properly on Windows machine because it utilizes a Linux command.

_images/cpu-info.png

Table Of Contents

Previous topic

Creating and Saving Code Modules

Next topic

Epilogue

This Page