2. Tkinter

Tkinter is a Python module for the TK-toolkit.

2.1. Hello, world

A first "Hello world" looks like this:
#!/usr/bin/python
import Tkinter as tk
root = tk.Tk()
w = tk.Label(root, text="Hello world!")
w.pack()
root.mainloop()

which gives the following window:

Which is nice. But what did we do?
import Tkinter as tk

So we imported a module Tkinter and we say we'd like to call it tk from now on. It is possible to just import Tkinter but then, instead of using root = tk.Tk() you would use root = Tkinter.Tk() and so on.

A lot of the rest is fairly standard Tk. So compare
Python
TCL/Tk
root = tk.Tk()
w = tk.Label(root, text="Hello world!")
label .hello -text "Hello, World!"
w.pack()
pack .hello

and you'll see it is basically the same.

At the end, we do the root.mainloop() which starts the tk event loop. This is the moment that Python starts the tk loop. In TCL, this is done automatically.

2.2. Hello again

Python uses classes. That means that you generally create classes to do something for you.
#!/usr/bin/python
from Tkinter import *
class App:
def __init__(self, master):
    self.frame = Frame(master)
    self.frame.pack()
    self.button = Button(
    self.frame, text="QUIT", fg="red", command=self.frame.quit
    )
    self.button.pack(side=LEFT)
    
    self.hi_there = Button(self.frame, text="Hello", command=self.say_hi)
    self.hi_there.pack(side=LEFT)
def say_hi(self):
    print "hi there, everyone!"
root = Tk()
app = App(root)
root.mainloop()
root.destroy()

Let's analyze what we did. First, we created a class App with two methods,
  • init
  • say_hi

The method __init__ is a standard name. The method gets called after the object is created. __init__ is not (as some less rigorous people seem to think) a constructor. The real constructor is called __new__(cls, *args, **kwargs) In general, Python uses a lot of __init__ and very seldom __new__.

The first argument to all methods is self. The reason for this is that Python likes to state all sort of things explicitly. The fact that there is an inconsistency between the calling of a method (where self is always missing) and the declaration does not seem to be a problem. So we need to learn when explicit is required and when not.

The App object contains a number of Tk objects:
  • frame (which is a Frame)
  • button with quit
  • button with hello

All of these objects get a prefix of self because, in Python, there is no explicit variable declaration.

It is worth noting that the self.frame.quit quits the application (the main loop)

2.3. Tkinter widgets

Most of the widgets are the same as in Perl/Tk or TCL/Tk, and the syntax example with the buttons above should make them usable.
Widget
Description
Button
A simple button
Canvas
Structured graphics.
Checkbutton
Represents a variable that can have two distinct values.
Entry
A text entry field.
Frame
A container widget.
Label
Displays a text or an image.
Listbox
Displays a list of alternatives.
Menu
A menu pane.
Menubutton
A menu button.
Message
Display a text.
Radiobutton
Represents one value of a variable that can have one of many values.
Scale
Allows you to set a numerical value by dragging a “slider”.
Scrollbar
Standard scrollbars for use with canvas, entry, listbox, and text widgets.
Text
Formatted text display.
Toplevel
A container widget displayed as a separate, top-level window.
LabelFrame
A variant of the Frame widget that can draw both a border and a title.
PanedWindow
A container widget that organizes child widgets in resizable panes.
Spinbox
A variant of the Entry widget for selecting values from a range or an ordered set.

As an example, a simle way to display an image is: