tkinter graphical interface programming

Use Python's own tkinter library, simple but crude

Graphical Interface Programming Essentials

Controls, Layouts, Event Response, Dialogs

tkinter control import tkinter as tk; tkinter extended control from tkinter import ttk

# The controls in tk are available in ttk, and are more beautiful, the usage is basically the same as tk, and ttk has a few more controls

# tkinter的常用控件
import tkinter as tk

win = tk.Tk() #生成一个窗口
tk.Label(win,.....) #在窗口win上生成一个Label,该Label的母体是win
ckb = tk.Checkbutton(win,.....) #在窗口上生成一个Checkbutton
frm = tk.Frame(win,.....) #在窗口上生成一个Frame
bt = tk.Button(frm,......) #在frm上生成一个Button

Generate windows with grid layout

In the grid layout, the default incremental allocation weight of rows and columns is 0, and the width and height will not change with the window size. You can set the incremental distribution weight to 1 to make the grid automatically change with the window size.

win.columnconfigure(0,weight = 1)  # 指定第0列增量分配权重为1 
win.rowconfigure(0,weight = 1)  # 指定第0行增量分配权重为1 

The sticky parameter of the grid() function, sticky indicates the "edge mode" of the control in the cell, that is, whether to stick to the four sides of the cell.
This parameter can be a string containing one or more of the four characters "E", "W", "S", "N"

Use Frame controls for layout

  • There are too many controls, it is very troublesome to count each control row, column, rowspan, columnspan
  • Controls can also be placed on the Frame control, which can be used as a bottom plate
  • You can set the grid on the Frame control for Grid layout and place multiple controls
import tkinter as tk
win = tk.Tk()
win.title('人事系统')
frm01Red = tk.Frame(win,bg="red",highlightthickness=2) #背景红色,边框宽度2
frm01Red.grid(row=0,column=1,columnspan=2,sticky="WE")
tk.Label(frm01Red, text="姓名:").grid(row=0,column=0,padx=6,pady=6)  # padx、pady指定水平和垂直方向上的外边距
tk.Entry(frm01Red).grid(row=0,column=1,padx=6,pady=6)
tk.Label(frm01Red, text="手机号:").grid(row=0,column=2,padx=6,pady=6)
tk.Entry(frm01Red).grid(row=0,column=3,padx=6,pady=6)
tk.Button(frm01Red,text="更新").grid(row=0,column=4,padx=6,pady=6)
win.mainloop()

 Overview of Control Properties and Event Responses

  • Some controls have functions that can be used to set and get their properties, or get and set their properties in the form of dictionary subscripts
lbHint = tk.Label(win,text = "请登录")
lbHint["text"] = "登录成功!" #修改lbHint的文字
txt = tk.Text(win)
txt.get(0.0, tk.END)) #取全部文字
  •  Some controls must be associated with a variable, to get or set the variable value is to get or set the property of the control
s = tk.StringVar()  # tk里的字符串型变量
s.set("sin(x)")  # 对s进行赋值
tk.Entry(win,textvariable = s)
print(s.get())  # 获取编辑框里的文字
  • When creating some controls, you can use the command parameter to specify the event response function of the control
tk.Button(win,text="显示函数图",command = myfunc) #myfunc是函数名
tk.Checkbox(win,text="显示函数图",command = lambda:print("hello"))  # command对应的函数没有参数
  •  You can use the bind function of the control to specify the event response function 
lb = tk.Label(win,text="something")
lb.bind("<ButtonPress-1>",mouse_down) #鼠标左键按下事件
lb.bind("<Double-Button-1>", lambda e:btAdd_click())  # 鼠标双击事件,响应函数是lambda表达式,带尖括号的事件,响应函数必须有一个参数

Guess you like

Origin blog.csdn.net/m0_46303430/article/details/125889237