There are several different GUI libraries available in Python, but tkinter is the one that ships with the standard library, and is extremely common in use.
Every UI element supported by tkinter is a widget. Widgets have a formal hierarchy, with each having a parent widget and zero or more children. The exception is the root widget, which is an application window.
Each widget has configuration parameters, which are stored in a dictionary. Let's take a look at a simple app (that doesn't really do anything):
from tkinter import *
from tkinter import ttk # Themed versions of widgets to match the OS.
root = Tk()
button = ttk.Button(root, text='Click Me')
button.pack()You can see that one of the parameters of the button widget is text, which we set when we created the button. You can also change configuration after creation, either by using the configuration parameter's name
print(button['text'])
button[text] = 'Press Me'
# Or use the config method
button.config(text='Push Me')
# Return all the configuration objects
print(button.config())Widgets have tcl names that are assigned automatically by tkinter. Every widget is added to a hierarchy of widgets, so they aren't garbage collected until the root object goes out of scope.
You can get the tcl names for objects, which are randomly assigned, by casting the object as a string. The root is always named '.'.
The issue of where widgets are placed relative to other widgets is a complicated one, and it is handled by the geometry manager.
Widgets have a master/slave relationship. Master widgets control the placement of their slaves.
Three geometry managers to choose from:
- Pack: simplest and most automated. You can specify an edge of the parent widget on which to pack the widget.
- Grid: defines rows and columns for the parent widget, and arranges the children on that grid.
- Place: lets you use relative or absolute coordinates to place each widget within its parent.
You can use multiple geometry managers within a single application, but you must use a single manager for each parent object.
Like Windows programming, tkinter uses events that you listen for to process user input and other interactions.
You start your event handling with the Tk.mainloop function.
Keep handlers short and sweet to avoid UI lag.
Some widgets have command callbacks that you set as properties of their objects, while others (like labels) don't have an explicit command associated with them. Those widgets can still be handle events by using the bind method.
Contains text or an image.
properties:
| text | the text to display |
| wraplength | the number of pixels at which to wrap the text |
| justify | [LEFT, RIGHT, CENTER] |
| foreground | text color, use hex value or name of common colors in a string |
| background | background color |
| font | `font = ('Courier', 18, 'bold')` |
| image |
takes a PhotoImage object containing the image you want to use You need to first create a PhotoImage object, by passing it the filename of the image to use: `my_image = PhotoImage(file='C;\\Path\\file.gif')`
NOTE When you assign an image to a label, it does not make a copy or hold a reference to the image object. Instead, it uses the address. This means that when the PhotoImage variable goes out of scope, Python will discard the variable and the image won't work anymore. Be careful where you hold the reference to the image to keep everything working.
A good practice is to store a reference to the image object in the label object, taking advantage of Python's flexible objects:
|
| compound |
['text', 'image', 'center', 'top', 'bottom', 'right', 'left'] configures how to deal with labels with both text and image values, either showing just one of them, centering the text atop the image, or placing the text next to the image on the specified side. |
root = Tk()
button = ttk.Button(root, text='Click Me')
button.pack()
def callback():
print('Clicked!')
button.config(command=callback)You can simulate a button click by calling the invoke() method.
button.state(['disabled'])
button.instate(['disabled']) returns Boolean.
properties:
| command | The function that gets called whenever the button is clicked. |
|