Python GUI Programming (Tkinter)
Python provides several libraries for developing graphical interfaces. Some commonly used Python GUI libraries are as follows:
-
Tkinter: The Tkinter module (Tk interface) is the interface to Python's standard Tk GUI toolkit. Tk and Tkinter can be used on most Unix platforms, as well as on Windows and Macintosh systems. Later versions of Tk 8.0 can implement native window styles and run well on the vast majority of platforms.
-
wxPython:wxPython is an open-source software and an excellent GUI graphics library for the Python language, allowing Python programmers to easily create complete, fully functional GUI user interfaces.
-
Jython:Jython programs can integrate seamlessly with Java. Apart from some standard modules, Jython uses Java's modules. Jython has almost all the modules of standard Python that do not depend on the C language. For example, Jython's user interface uses Swing, AWT, or SWT. Jython can be compiled dynamically or statically into Java bytecode.
Tkinter Programming
Tkinter is Python's standard GUI library. Using Tkinter, Python can quickly create GUI applications.
Since Tkinter is built into the Python installation package, once Python is installed you can import the Tkinter library, and IDLE is also written with Tkinter. For simple graphical interfaces, Tkinter is more than adequate.
Note: In Python 3.x the library name is tkinter, that is, with the first letter T in lowercase.
import tkinter
Create a GUI program
- 1. Import the Tkinter module
- 2. Create widgets
- 3. Specify the master of this widget, that is, which one this widget belongs to
- 4. Tell the GM (geometry manager) that a widget has been created.
Example (Python3.x)
#!/usr/bin/python3
import
tkinter
top
=
tkinter
.
Tk
(
)
Enter the message loop
top
.
mainloop
(
)
Example (Python2.x)
#!/usr/bin/python
-- coding: UTF-8 --
import
Tkinter
top
=
Tkinter
.
Tk
(
)
Enter the message loop
top
.
mainloop
(
)
The result of executing the above code is shown below:

Example
#!/usr/bin/python
-- coding: UTF-8 --
Python 2.x import method
from
Tkinter
import
Import the Tkinter library
Python 3.x import method
#from tkinter import *
root
=
Tk
(
)
Create the background color of the window object
Create two lists
li
=
[
'
C
'
,
'
python
'
,
'
php
'
,
'
html
'
,
'
SQL
'
,
'
java
'
]
movie
=
[
'
CSS
'
,
'
jQuery
'
,
'
Bootstrap
'
]
listb
=
Listbox
(
root
)
Create two list widgets
listb2
=
Listbox
(
root
)
for
item
in
li
:
Insert data into the first widget
listb
.
insert
(
0
,
item
)
for
item
in
movie
:
Insert data into the second widget
listb2
.
insert
(
0
,
item
)
listb
.
pack
(
)
Place the widgets in the main window
listb2
.
pack
(
)
root
.
mainloop
(
)
Enter the message loop
The result of executing the above code is shown below:
Tkinter components
Tkinter provides various widgets, such as buttons, labels, and text boxes, for use in a GUI application. These controls are commonly referred to as widgets.
There are currently 15 Tkinter widgets. We present these widgets along with a brief introduction in the table below:
| Widget | Description |
|---|---|
| Button | Button widget; displays a button in the program. |
| Canvas | Canvas widget; displays graphical elements such as lines or text |
| Checkbutton | Checkbutton widget; provides multiple-selection checkboxes in the program |
| Entry | Entry widget; used to display simple text content |
| Frame | Frame widget; displays a rectangular area on the screen, often used as a container |
| Label | Label widget; can display text and bitmaps |
| Listbox | Listbox widget; the Listbox widget is used to display a list of strings to the user |
| Menubutton | Menubutton widget, used to display menu items. |
| Menu | Menu widget; displays menu bars, drop-down menus, and pop-up menus |
| Message | Message widget; used to display multi-line text, similar to the label |
| Radiobutton | Radiobutton widget; displays a single-choice button state |
| Scale | Scale widget; displays a numeric scale, restricting output to a range of numbers |
| Scrollbar | Scrollbar widget, used when content exceeds the visible area, such as in a listbox. |
| Text | Text widget; used to display multi-line text |
| Toplevel | Toplevel widget; used to provide a separate dialog box, similar to Frame |
| Spinbox | Spinbox widget; similar to Entry, but allows specifying a range of input values |
| PanedWindow | PanedWindow is a window layout manager widget that can contain one or more child widgets. |
| LabelFrame | labelframe is a simple container widget, often used for complex window layouts. |
| tkMessageBox | Used to display message boxes for your application. |
Standard Attributes
Standard attributes are the common attributes of all widgets, such as size, font, and color.
| property | Description |
|---|---|
| Dimension | Widget size; |
| Color | Widget color; |
| Font | Widget font; |
| Anchor | Anchor; |
| Relief | Widget style; |
| Bitmap | Bitmap; |
| Cursor | Cursor; |
Geometry management
Tkinter widgets have specific geometry management methods that manage the organization of the entire widget area. The following are the geometry manager classes exposed by Tkinter: pack, grid, place
| Geometry methods | Description |
|---|---|
| pack() | Pack; |
| grid() | Grid; |
| place() | Place; |