Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

python GUI Programming (Tkinter)


May 10, 2021 Python2


Table of contents


python GUI Programming (Tkinter)

Python provides libraries for multiple graphical development interfaces, and several common Python GUI libraries are as follows:

  • Tkinter: T he Tkinter module ("Tk interface") is the interface to Python's standard Tk GUI toolkit. T k and Tkinter can be used on most Unix platforms, as well as in Windows and Macintosh systems. S ubsequent versions of Tk8.0 achieve local window style and run well on most platforms.
  • wxPython: wxPython is an open source software that is an excellent GUI graphics library in the Python language, allowing Python programmers to easily create a complete, functionally keyed GUI user interface.
  • Jython: J ython programs can integrate seamlessly with Java. I n addition to some standard modules, Jython uses Java modules. J ython has almost all the modules in the standard Python that are not dependent on the C language. F or example, Jython's user interface will use Swing, AWT, or SWT. J ython can be compiled dynamically or statically into Java bytecode.

Tkinter programming

Tkinter is Python's standard GUI library. Python uses Tkinter to quickly create GUI applications.

Because Tkinter is built into Python's installation package, you can import the Tkinter library as soon as Python is installed, and IDLE is written from Tkinter, Tkinter is still well-prepared for a simple graphical interface.

Note:P version of ython3.x uses a library called tkinter, which means that the initials T are lowercase:

import tkinter

Create a GUI program

  • 1, import Tkinter module
  • 2, create controls
  • 3, specify the master of this control, that is, which control belongs to
  • 4, tell gm (geometry manager) that a control has been produced.

Instance:

#!/usr/bin/python

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

The above code execution results are as follows:

python GUI Programming (Tkinter)

Example 2:

#!/usr/bin/python
# -*- 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:                 # 第一个小部件插入数据
    listb.insert(0,item)

for item in movie:              # 第二个小部件插入数据
    listb2.insert(0,item)

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

The above code execution results are as follows:

python GUI Programming (Tkinter)



The Tkinter component

Tkinter offers a variety of controls such as buttons, labels, and text boxes that are used in a GUI application. These controls are often referred to as controls or parts.

There are currently 15 Tkinter parts. We present these parts as well as a brief introduction in the table below:

Control Describe
Button button control;
Canvas A canvas control that displays graphic elements such as lines or text
Checkbutton Multi-box controls;
Entry To display simple text content
Frame Frame control, a rectangular area that appears on the screen, mostly as a container
Label Label controls;
Listbox List box controls;
Menubutton Menu button controls, due to the display of menu items.
Menu Menu controls, display menu bar, drop-down menu, and pop-up menu
Message Message control, which is used to display multiple lines of text, similar to label
Radiobutton A single button control that displays the status of a single button
Scale Range control, which displays a numeric scale that is a numeric interval that limits the output to a range
Scrollbar Scroll bar controls that are used when content exceeds the visual area, such as list boxes.
Text A text control that displays multiple lines of text
Toplevel Container control, which provides a separate dialog box similar to Frame
Spinbox Input control;
PanedWindow PanedWindow is a plug-in for window layout management that can contain one or more child controls.
LabelFrame labelframe is a simple container control. Commonly used with complex window layouts.
tkMessageBox The message box used to display your application.

The standard property

Standard properties are common to all controls, such as size, font, color, and so on.

Property Describe
Dimension Control size;
Color Control color;
Font Control font;
Anchor Anchor point;
Relief Control style;
Bitmap Bitmap;
Cursor Cursor;

Geometric management

Tkinter controls have specific geometric state management methods that manage the entire control area organization, and one is the geometric management classes exposed by Tkinter: packages, meshes, locations

Geometric methods Describe
pack() Packaging;
grid() Grid;
place() Location;