Python Notes and 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)
        
        self.add(self.button)

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

win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
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 http://www.tkdocs.com/tutorial/index.html is good and applies to Python 3.

Here’s an older one: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html. It’s for Python 2.x, but good coverage of ttk.

See also http://stackoverflow.com/questions/673174/file-dialogs-of-tkinter-in-python-3/673309#673309

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()
        self.style = ttk.Style()
        available_themes = self.style.theme_names()
        random_theme = random.choice(available_themes)
        self.style.theme_use(random_theme)
        self.root.title(random_theme)

        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
        button.pack(pady=8)

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

app = App()
app.root.mainloop()