Intro to Tkinter

From oldwiki.scinet.utoronto.ca
Jump to navigation Jump to search

Overview

This is a ~2 hour class that will introduce Graphical User Interface (GUI) programming using the Python Tkinter module. Topics covered will include:

  • The creation of widgets
  • Geometry Managers
  • Pop-up windows
  • Menus, bindings
  • GUIs and threads

Please note that beginner-intermediate experience with Python will be assumed. If you are new to Python, be sure to brush-up before class.

Laptop-preparation Instructions

This course will feature in-class hands-on work, to be performed on your laptop. Before coming to the class, please ensure that the following are installed on your laptop:

  • Some version of Python. The class will be taught using Python 2.7.X, but any more-recent version of Python (2.6.X, 3.3.X) should work.
  • Tkinter. Be sure to test the installation to make sure that it works correctly. This can be done by typing
    import Tkinter
    from a Python command line. If that doesn't work you are probably running Python 3.3.X. Try the following instead:
    import tkinter
    .
  • Python Imaging Library (PIL). Again, this can be tested by typing
    import PIL
    and
    import _imaging
    from the Python command line. If you have problems installing this package in particular, see below.

If you can't get the aforementioned software working on your laptop, please contact us and we will assist you.


Installing PIL on Windows

This section deals with following error case:

  • you are running Windows
  • you have installed Python 2.7.X
  • you attempt to install PIL
  • you get the following error:
    "Python version 2.7 required, which wasn't found in the registry"

The problem is that the PIL installer can't find the registry keys, because the Python installer put the values in the 32-bit-OS-version path instead of the 64-bit-OS-version path. To fix this, try the following:

  • Open up cmd.exe. This is done by going to the Start menu and typing
    cmd.exe
    in the "Search programs and files" box, and then hitting "enter".
  • Type the following command into the cmd prompt:
    reg copy HKLM\SOFTWARE\Python HKLM\SOFTWARE\Wow6432Node\Python /s

This should copy the registry keys to the correct path.

I can't "import _imaging"

If you type

import _imaging

from the Python prompt and you get the error message

Module use of pythonXX.dll conflicts with this version of Python

then the most likely cause of the problem is the mixing of 32-bit and 64-bit versions of Python and PIL. Make sure that your versions are compatible with each other. You are most likely to see this problem on Windows.


If you instead get the message

undefined symbol: ImagingRadianceEncode

and you are running Mandrake Linux then you have successfully downloaded a broken PIL build. The solution is to either build PIL from the PythonWare sources, or make sure you have the latest version of the appropriate Mandrake package.

Helpful links