Python GUI Programming (Tkinter)

Python provides multiple libraries for graphical interface development. Several 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, and can also be applied in Windows and Macintosh systems. Later versions of Tk8.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 be seamlessly integrated with Java. Except for some standard modules, Jython uses Java modules. Jython has almost all the modules in standard Python that do not depend on the C language. For example, Jython's user interface will use Swing, AWT, or SWT. Jython can be compiled dynamically or statically into Java bytecode.


Tkinter Programming

Tkinter is the standard GUI library for Python. Python can use Tkinter to quickly create GUI applications.

Since Tkinter is built into the Python installation package, as long as Python is installed, you can import the Tkinter library. Moreover, IDLE is also written in Tkinter, and Tkinter can still handle simple graphical interfaces with ease.

Note: The library name used in the Python3.x version is tkinter, that is, the first letter T is lowercase.

import tkinter

Create a GUI Program

  • 1. Import the Tkinter module
  • 2. Create the widget
  • 3. Specify the master of this widget, i.e., which one this widget belongs to
  • 4. Tell 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 execution result of the above code is shown in the figure below:

tkwindow

Example

#!/usr/bin/python # -*- coding: UTF-8 -*- # Python2.x import method from Tkinter import * # Import the Tkinter library # Python3.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 listbox 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 into the main window listb2.pack() root.mainloop() # Enter the message loop

The execution result of the above code is shown in the figure below:


Tkinter Widgets

Tkinter provides various controls, such as buttons, labels, and text boxes, used in a GUI application. These controls are often called widgets or components.

There are currently 15 Tkinter widgets. We present these widgets and a brief introduction in the following table:

WidgetDescription
ButtonButton widget; displays a button in the program.
CanvasCanvas widget; displays graphical elements such as lines or text
CheckbuttonCheckbutton widget; used to provide multiple choice boxes in a program
EntryEntry widget; used to display simple text content
FrameFrame widget; displays a rectangular area on the screen, mostly used as a container
LabelLabel widget; can display text and bitmaps
ListboxListbox widget; the Listbox window widget is used to display a list of strings to the user
MenubuttonMenubutton widget; used to display menu items
MenuMenu widget; displays menu bar, drop-down menus, and pop-up menus
MessageMessage widget; used to display multi-line text, similar to Label
RadiobuttonRadiobutton widget; displays a single-selection button state
ScaleScale widget; displays a numerical scale, providing a numeric interval with a limited range for output
ScrollbarScrollbar widget; used when the content exceeds the visible area, such as a listbox.
TextText widget; used to display multi-line text
ToplevelToplevel widget; used to provide a separate dialog, similar to Frame
SpinboxSpinbox widget; similar to Entry, but can specify a range of input values
PanedWindowPanedWindow is a window layout management plugin that can contain one or more child widgets.
LabelFramelabelframe is a simple container widget. Often used for complex window layouts.
tkMessageBoxUsed to display a message box for your application.

Standard Attributes

Standard attributes are the common attributes of all widgets, such as size, font, and color, etc.

Attribute 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 state management methods, managing the organization of the entire widget area. The following are the geometry management classes exposed by Tkinter: pack, grid, place

Geometry Method Description
pack() Pack;
grid() Grid;
place() Place;
Other Extensions