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, a directory browser and vrex. In addition there is complex, which is kind of a gee whiz example.
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.
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
The main portion of the GUI is a three pain 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.
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.
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.
vrex is composed of two modules vrex.py which is the main GUI and vrex_help.py.
The main GUI is made up of a menu a paned window with three adjustable sub-windows each of which contains a scrolled test window and a row of buttons which control the action of the program. The GUI may be resized as may the help window.
It illustrates putting scrolled text boxes into panes of a paned window. I also show how to synchronize scrolled text boxes so that selecting a line in one paned window with Button-1 will cause the corresponding line to be visible in the other window.
For the subpattern buttons in the GUI, I used Button widgets rather than TButton widgets because I wanted the labels to match the colors of the regular expression grouping and the corresponding match strings. In addition, I wanted larger fonts than the default. I changed the colors of the Button backgrounds with the Attribute Editor.
In the examples/vrex subdirectory there are two files v.tcl and v_help.tcl which can be separately specified as parameters to page and used to build the initial version of v.py and v_help.py. These are executable programs which don’t really do anything other than show the GUI. One then needs to fill them in with the code that provides the function of the application and links the two GUI’s together. I have called them v.tcl rather than vrex.tcl, otherwise generating Python from them would over write the completed example.
After firing up vrex.py, try dragging the corner to see how well the relative placement works.
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.
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.
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.
As above for the themed widgets.