Latest web development tutorials

python GUI Programming (Tkinter)

python provides several graphical development interface library, several commonly used Python GUI library as follows:

  • Tkinter: Tkinter module ( "Tk Interface") is an interface standard .Tk Python Tk GUI toolkit and Tkinter can in most Unix platforms, the same can be applied to both Windows and Macintosh systems, Tk8.0 follow. version can achieve local style window, and run well in most platforms.
  • wxPython: wxPython is an open source software, is a set of Python language excellent GUI graphics library that allows Python programmers to easily create a complete, full-function keys for GUI user interface.
  • Jython: Jython and Java programs can be seamlessly integrated. Except for some standard modules, Jython module using Java. Jython has almost standard Python does not depend on the C language all of the modules. For example, Jython user interface uses the Swing, AWT, or SWT. Jython can be dynamically or statically compiled into Java byte code.

Tkinter Programming

Tkinter standard Python GUI library. Python using Tkinter can quickly create GUI applications.

Since Tkinter is built into python installation package, then as long as you can import Tkinter installed Python libraries, but IDLE is written using Tkinter made for a simple graphical interface Tkinter still be able to cope.

Creating a GUI program

  • 1, import Tkinter module
  • 2, create the control
  • 3. Assign the control master, that is the control to which they belong
  • 4, told GM (geometry manager) has produced a control.


# -*- coding: UTF-8 -*-

import Tkinter
top = Tkinter.Tk()
# 进入消息循环

The results of the above code is executed as follows:


Example 2:

# -*- coding: UTF-8 -*-

from Tkinter import *           # 导入 Tkinter 库
root = Tk()                     # 创建窗口对象的背景色
                                # 创建两个列表
li     = ['C','python','php','html','SQL','java']
movie  = ['CSS','jQuery','Bootstrap']
listb  = Listbox(root)          #  创建两个列表组件
listb2 = Listbox(root)
for item in li:                 # 第一个小部件插入数据

for item in movie:              # 第二个小部件插入数据

listb.pack()                    # 将小部件放置到主窗口中
root.mainloop()                 # 进入消息循环

The results of the above code is executed as follows:

Tkinter module

Tkinter provides a variety of controls, such as buttons, labels and text boxes, a GUI application use. These controls are usually referred to as controls or components.

There are 15 kinds of Tkinter parts. We make these components as well as a brief description, in the following table:

Controls description
Button Button control; display button in the program.
Canvas Canvas control; display graphical elements such as lines or text
Checkbutton Checkbox controls; for providing a multiple choice box in the program
Entry Input control; for displaying simple text content
Frame Frame control; display a rectangular area on the screen, usually used as a container
Label Tab control; can display text and bitmaps
Listbox List box control; in Listbox widget is used to display a list of strings to the user
Menubutton Menu button controls, because the menu item is displayed.
Menu Menu control; display the menu bar, drop-down menus and pop-up menus
Message Message control; to display multiple lines of text more similar, with label
Radiobutton Radio button controls; displays a radio button states
Scale Range control; display a numerical scale, limited range of digital output interval
Scrollbar Scrollbar control when the content exceeds the visualization area use, such as list boxes. .
Text Text controls; used to display multiple lines of text
Toplevel Container control; to provide a separate dialog box, and compare similar Frame
Spinbox Input controls; similar to the Entry, but you can specify the input range of values
PanedWindow PanedWindow is a window layout management plug-ins, you can contain one or more child controls.
LabelFrame labelframe is a simple container control. Common and complex window layouts.
tkMessageBox It displays a message box for your application.

Standard property

Standard attribute that is common property of all the controls, such as size, font and color, and so on.

Attributes description
Dimension Control size;
Color Control color;
Font Control font;
Anchor Anchor;
Relief Control style;
Bitmap bitmap;
Cursor cursor;

Geometry Management

Tkinter control has specific geometric state management methods, management controls the entire regional organizations, what is Tkinter disclosed geometry manager class: pack, grid position

Geometric method description
pack () package;
grid () grid;
place () position;