Table of contents
1. Graphical user interface and tkinter
In python 图形用户界面
, you can GUI
develop graphical page applications through , tkinter
which is the default GUI of python. 内置模块
It can be said that it is one 标准GUI库
of the best. Compared with other GUIs, it is simple to operate and portable (can be used on most Unix platforms). Use, can also be applied to Windows and Mac systems), etc., you can import this module through import thinter
or .from tkinter import *
import thinter
from tkinter import *
2. The root window of tkinter
(1) Creation of root window
Before programming the graphical user page, you must first 创建一个根窗口(主窗口)
create a root window object through the constructor of the TK class in the tkinter module. Each program can only have one root window, but multiple sub-windows are allowed. To ensure that the graphical user page It is always running. You can use the mainloop() method to enter the message loop.
For example, the following python code:
import tkinter
window=tkinter.Tk() #创建一个根窗口对象window
window.mainloop() #进入消息循环
The running results are as follows, a root window is generated:
(2) Basic methods of root window
You can set the root window through basic methods, as follows:
1. title()
Used to set the name of the window;
2. resizable()
Used to set the adjustability of the window, which is adjustable by default and does not need to be set;
3. geometry()
Used to set the root window The size, this method receives a string in the format of width × height + x-axis offset + y-axis offset.
4. quit()
Used to exit the window;
5. updata()
Used to refresh the page.
For example, the following python code creates a root window object window, sets the root window name and makes it adjustable:
import tkinter
window = tkinter.Tk() # 创建一个根窗口对象window
window.title("我的GUI页面") # 设置根窗口的名称
window.resizable() # 使根窗口可调,即可任意调节其长宽
window.mainloop() # 进入消息循环
The running results are as follows:
the window can be adjusted:
3. Geometric Layout Manager
Components in tkinter can be laid out through the geometric layout manager, which is divided into three types: pack, grid and place 同一父窗口中只能使用一种几何布局管理器
.
(1) Pack layout
The pack layout can be regarded as a container/package. It is called to pack()方法
add the component to the parent component. It has several properties:
1. expand sets the filling method of the component;
2. fill sets whether the component fills additional space. The value can be none. , x, y or both;
3. side sets the distribution mode of components. The value can be Top, bottom, left or right. The default value is top.
(2) grid layout
The call grid()方法
can be used to divide the parent component into a two-dimensional table, which has several properties:
1. row sets the row of the component;
2. column sets the column of the component;
3. rowspan sets the number of rows occupied by the component.
(3) place layout
The call place方法
can place the component in a specific position. It has several attributes:
1. anchor sets the position of other options of the component;
2. relx and rely set the position relative to the window width and height, and the value range is [0,1.0 ], where relx=0.5, rely=0.5 is in the center;
3. x, y set the coordinates of the absolute layout, the unit is px.
4. tkinter component
(1) Label
Label labels are used 显示信息
to display text and bitmaps. Labels are created through the constructor Label() of the Label class.
This component has the following commonly used properties:
Attributes | Function |
---|---|
anchor | Set the position of text and images, default is center |
background | Set the background color of the label |
borderwidth | Set the label border width (px), the default value is 2px |
foreground | Set foreground color |
height | Set the height of the label |
width | Set the width of the label |
image | Set image to display on label |
Padx | Additional padding to the left and right of the text |
Paddy | Additional padding above and below text |
state | Label status |
justify | Set alignment, default is center |
text | Set text |
relief | Set the label style, the default is flat |
For example, the following python code: |
import tkinter
window = tkinter.Tk()
a = tkinter.Label(window, anchor="s", height=20, width=30, text="HELLO WORLD!", background="yellow") # 创建一个标签
a.pack() # 通过pack()方法布局
window.mainloop()
The running results are as follows:
(2) Button
A button object can be created through the constructor Button() of the Button class. This component has the following commonly used properties:
Attributes | Function |
---|---|
activebackground | The background color of the button when the mouse is over |
activeforeground | The foreground color of the button when the mouse is placed on it |
background | The background color of the button |
borderwidth | Border width, default is 2 pixels |
foreground | Normal foreground (text) color |
height | high |
width | width |
image | image to display |
Padx | Additional padding to the left and right of the text |
Paddy | Additional padding above and below text |
text | The text content of the button |
command | Action triggered when button is clicked |
For example, the following python code creates a button, the button is black, and the content is displayed when the button is clicked:
import tkinter
window = tkinter.Tk()
b = tkinter.Button(window, text="HELLO WORLD!", background="black")
b.pack()
window.mainloop()
The running results are as follows:
click the button and the button displays the content:
(3) Text box
For example, the following python code:
import tkinter
widows=tkinter.Tk()
E=tkinter.Entry(widows,width=50,fg='red',background='blue')
E.pack()
widows.mainloop()
The running results are as follows:
You can enter text in the text box:
……