May 10, 2021 Python2
Python provides libraries for multiple graphical development interfaces, and several common Python GUI libraries are as follows:
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
Instance:
#!/usr/bin/python
import Tkinter
top = Tkinter.Tk()
# 进入消息循环
top.mainloop()
The above code execution results are as follows:
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:
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. |
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; |
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; |