Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python GUI Programming (Tkinter)

Python GUI Programming (Tkinter)

Python provides several libraries for developing graphical interfaces. Some commonly used Python GUI libraries are as follows:


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

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:

tkwindow

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;