Isosurface Generation Using VTK




Using Python and Tkinter



2.1. Python Basics


With this portion of the tutorial, it is possible to either type code examples line-by-line directly into the Python interpreter and observe what happens and check the state of different variables, or it is also possible to save the code in a file using your favorite text editor or IDLE, the Python text editor that comes with the Python distribution, and execute the code in the file all at once.  It should be noted that when using Tkinter, using the Python interpreter does not work particularly well, and its use should be discouraged.  That is the power of Python— rapid prototyping coupled with lots of functionality.  Python was written using C/C++.  As stated earlier, if you are not comfortable using Python completely on its own, there is a Java version of Python available called Jython that can be downloaded from  This will allow Python programs to incorporate functionality from other Java classes in addition to utilizing the Swing UI for the program’s GUI. 


Since the author is by no means an expert at Python, the old-style of object-orientated (OO) programming using Python will be described.  If the reader is more familiar with the new style of Python OO programming, the reader is encouraged to discard information given here and utilize what is already known.  Additionally, since the author is not familiar with making a Python program that derives from multiple files, the code will be assumed to reside in a single file that could consist of multiple separate classes.


When a programmer creates this huge single file program, it is crucial that the code is organized into classes.  A good guideline is to divide the project into the following classes:


  • GUI building (see the next section for more details)
  • Visualization (the VTK pipelines)
  • Data model (the actual data being visualized; especially necessary when it is necessary to preprocess the data before putting it into the VTK pipelines)
  • One class for each dialog planned
  • Main window


It is very easy to get sloppy with coding when one might be more accustomed to strongly typed languages like Java and C++.  For this reason, listed below are some tips to remember when writing a VTK program using Python (or any language for that matter):

  • Create “class-level” static variables by assigning values to variables before the class constructor named __init__.
  • Private member variables are defined as self.__variableName.  This same naming convention can be used to make member functions private as in the case of def __functionName( self, parameters ).
  • Even though one may not know Hungarian notation, make up your own notation that indicates the type of as variable in a variable name as well the return type for a function in a function name.
  • Use all upper case letters for constant variables.


Remember that sometimes only basic syntax errors will be received during execution.  Some obvious errors that would likely be caught by strongly-typed language compilers during compilation will only be caught by Python at run-time when that particular code path is executed.  Do not become overly concerned when this happens.  So, just when you thought it was not possible to have null pointers or dangling pointers, you instead will have errors originating from not declaring a variable or misspelling the variable when you intended another name.  Other errors can be more subtle but traced to other sources.



<<< Previous

Using Python and Tkinter



Table of Contents



Next >>>

Tkinter Basics