Isosurface Generation Using VTK




Using Python and Tkinter



2.2. Tkinter Basics


As stated before, it would probably be better to use wxPython as described in the introduction to this chapter.  However, if you have gotten this far, you have probably decided that Tkinter is the GUI that you will use.  Before going any farther, the author strongly recommends that you download a PDF file from the web entitled “An Introduction to Tkinter”.  This guide will describe many of the functions of different GUI widgets in addition to providing nice composition widgets that try to get around the limitedness of Tkinter.  It also provides some key information to why Tkinter sometimes behaves strangely.  Another important part of this tutorial is a GUI builder which the author made that can be viewed here.  You can modify it for your own purposes.  It was created in order to take the tediousness out of writing code to make a GUI work.  Additionally, there are some composite widgets such as a listbox with a scrollbar and a slider with a value label not tied to the location of the “moving” part of the slider. 


In the next subsections, there will be a mini-tutorial for creating a main Tkinter window with a menu bar and a mini-tutorial for making an RGB color changer.  In the next chapter, there will also be more mini-tutorials.  The idea is that these mini-tutorials will help you understand how to use the objects, understand the basic abilities of an object, and be able to use the object in a bigger project (in this case, the isosurface tutorial in chapter 4).


Below are some helpful tips that will assist in the task of programming with Tkinter:

  • Try to avoid using any absolute placement of widgets because if the container widget is scaled, the contained widgets will not scale as you may want.  Also, avoiding absolute widget placement enforces a better design style.
  • When making the structure for the main window, consider using the packer widget placer.  This will allow an easier placement of the rendering window with any other materials in the window.  However, it should be noted that the author typically created a very simple main window with only a menu, the VTK render widget, and a label acting as a status bar.
  • When creating a dialog with many widgets, it may be better to use the grid widget placer.
  • Never have a modal dialog box bring up another modal dialog box.  The author has not been able to do this properly for the single time that he attempted to do this and is unable to explain why this does not work properly.  When in doubt, simply create sub-windows that do not behave as modal dialog boxes (i.e. modeless dialog boxes).
  • Make sure your dialog boxes are not destroyed when they are closed.  In this manner, the state of the dialog box is preserved between uses.  If that does not work, make a class that contains a dialog box and stores its state in that class rather than making a dialog box that encapsulates its own state.
  • Sometimes, widget callback functions will have too few arguments, and it will not be very easy to explain why.  Rather than ponder the imponderable, use the parameter *None as the last parameter in the function definition parameter list.  This will allow things to work properly.
  • There is no scrollable listbox, but it is possible to use my adaptation of a scrollable listbox from the Python in a Nutshell book.



<<< Previous

Python Basics



Table of Contents



Next >>>

Creating a Tkinter Main Window