Some Python Examples
← prev | next →     Top-level ToC     /gui.html     (printable version)

1 GTK+ 3

The most up-to-date tutorial for using GTK+ v3 with Python 3 is sebp/PyGobject-Tutorial.

Prereq: (on Debian) you’ll need the python3-gi package installed.

Here’s a Python 3 GObject “Hello World” app, lifted from the tutorial with only minor typographical edits:

#!/usr/bin/env python3

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class MyWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title='Hello World App')
        self.button = Gtk.Button(label='Hello')

        # Connect the "clicked" button event to the `on_button_clicked`
        # callback (and a signal-callback pair is made).
        self.button.connect('clicked', self.on_button_clicked)

    # A callback function.
    def on_button_clicked(self, widget):
        print('Hello, World!')

win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
Gtk.main()  # Start the main loop.

When the user does something — say, clicks a button — the main loop delivers an event to GTK. Widgets receive events, and when they do they often emit one or more signals (internal to GTK). When programming, you connect callback functions to various signals. That is, when the widget receives the event, a signal is emitted and your callback functions are called.

Widgets have properties — for example, a button has a “label” property — and you can specify properties in four ways:

  • in the ctor, ex., self.my_button = Gtk.Button(label='Hello')
  • self.my_button.set_label('Hello')
  • self.my_button.props.label = 'Hello'
  • self.my_button.set_property('label', 'Hello')

See the Python GTK+ 3 tutorial for further details.

2 Tk

The tkinter tut at is good and applies to Python 3.

Here’s an older one: It’s for Python 2.x, but good coverage of ttk.

See also

Install tkinter:

apt-get install python3-tk

This gets you ttk also, which comes with tkinter. tix is separate, and may be outdated (?)

This app (found posted at stackoverflow) lets you change (live) its own theme:

#!/usr/bin/env python3

import random
import tkinter
from tkinter import ttk
from tkinter import messagebox

class App(object):

    def __init__(self):
        self.root = tkinter.Tk() = ttk.Style()
        available_themes =
        random_theme = random.choice(available_themes)

        frm = ttk.Frame(self.root)
        frm.pack(expand=True, fill='both')
        # create a Combobox with themes to choose from
        self.combo = ttk.Combobox(frm, values=available_themes)
        self.combo.pack(padx=32, pady=8)
        # make the Enter key change the style
        self.combo.bind('<Return>', self.change_style)
        # make a Button to change the style
        button = ttk.Button(frm, text='OK')
        button['command'] = self.change_style

    def change_style(self, event=None):
        """set the Style to the content of the Combobox"""
        content = self.combo.get()
        except tkinter.TclError as err:
            messagebox.showerror('Error', err)

app = App()