Python Tutorial Part 6 C : Intro to tkinter


– no title specified

 

Python Course

Session 6c – Quick Introduction to tkinter

Table of Contents

tkinter

GUI Operation

A Simple tkinter Example

Useful References

 

tkinter

The tkinter module is provided in the Python standard library. It is a Python binding to the Tk GUI which is part of the Tcl/Tk language. There are numerous other GUIs with Python bindings.

Note that tkinter is named Tkinter in Python 2.

GUI Operation

A GUI program needs to be responsive to interactions with its GUI, such as a user making a “button press”. That generally means it’s not a good idea for the program to be absorbed in computations and not checking inputs for long periods of time.

This requires careful consideration of the design of the program. There are various ways to achieve responsiveness:

  • The program runs in a loop, performing short computations and checking for inputs.

  • The program is interrupt driven, temporarily switching to respond to the input interrupt.

  • The program runs a separate thread whose job is just to handle interrupts.

  • The program runs multiple separate threads to handle multiple interrupts simultaneously.

Which mechanism is used depends on the sophistication necessary for the particular program. Having a program sitting in a tight loop checking inputs is never a good idea (the processor gets very warm!). So even in the simplest case you should at least have a short sleep when there is no input to process. Unless that means you miss a very brief input.

A Simple tkinter Example

Here’s a very simple example which pops up a window containing a button “widget” that can be pressed. When pressed, a label widget appears displaying some text.

import tkinter as tk
from tkinter import ttk

root = tk.Tk()

root.title(“Greetings“)

def hi():

  ttk.Label(root, text=”Hi!”).grid(row=1, column=0)

ttk.Button(root, text=”You are very welcome to press this button“,
command=hi).grid(row=0, column=0)

root.mainloop()  # Gives up control to tk to handle events.

# You only get here when the window is killed.

Before pressing the button:

After pressing the button:

The program imports tkinter from the Python standard library. It provides Tcl/Tk style widgets. From tkinter we import ttk which provides widgets styled for the platform you’re running on.

It obtains the root of the widget hierarchy, and give it a window title. This is the toplevel window which contains everything that will be displayed.

It defines a button and arranges for the command function hi to be called when it is pressed.

It then invokes an event loop which causes the window to appear on screen, and which responds to any events on the window, such as a button press.

When the button is pressed the hi function is invoked which then defines a label widget which then appears below the button in the window.

Note the grid method, invoked on the button and label widget objects. Widgets have to be managed (arranged) otherwise they do not appear. There are three tkinter geometry managers: grid, pack and place.  Arranging widgets just how you want them is one of the most fiddly aspects of GUI design, and the grid geometry manager usually achieves what you want with the least effort.

Useful References

Graphical User Interfaces witk Tk – Python Standard Library

tkinter – Python interace to Tcl/Tk – Python standard Library

(With lots of useful references)

ActiveState Tcl User Guide

ActiveState Tk Manual

(If you install ActiveState Tcl on Windows, you get a nifty Tcl Help app that better presents the info of those two)

GPIO simulator

There are a number of GUI GPIO simulators on the web, mostly using tkinter; this is one.