Tkinter graphical interface design in Python (detailed tutorial) third, the characteristic attributes of tkinter common controls

Three, the characteristic attributes of tkinter common controls

3.1 Text input and output related controls

3.1.1 Label and Message

Text input and output controls usually include: label (Label), message (Message), input box (Entry), text box (Text). In addition to the aforementioned common attributes, they all have some characteristic attributes and functions.

  • Label and Message: Except for the difference between single line and multiple lines, the attributes and usage are basically the same, and they are used to present text information. It is worth noting that the attribute text is usually used for the fixed text of the instance when it is first rendered, and if it needs to change after the program is executed, you can use one of the following methods to achieve: 1. Use the configure() method of the control instance To change the value of the attribute text, the displayed text can be changed; 2. First, define a tkinter internal type variable var=StringVar() to change the displayed text.
    Look at the following example: make an electronic clock, use root's after() method to obtain the current time of the system every 1 second, and display it in the label.

Method 1: Use the configure() method or config() to achieve text changes.

#coding=utf-8
import tkinter
import datetime
Win = tkinter.Tk()
Win.title("时钟")
Win.geometry("650x60+200+200")

LabelTime = tkinter.Label(Win,text='',fg='blue',font=("黑体",50))

def GetTime():
    TimeStr = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    LabelTime.configure(text=TimeStr)  # 重新设置标签文本
    Win.after(1000, GetTime)  # 每隔1s调用函数 gettime 自身获取时间

LabelTime.pack()
GetTime()
Win.mainloop()

Insert picture description here
Method 2: Use textvariable variable attributes to achieve text changes

def GetTime2():
      TimeStr2.set(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))   # 获取当前时间
      Win.after(1000,GetTime2)   # 每隔1s调用函数 gettime 自身获取时间
TimeStr2=tkinter.StringVar()
LabelTime2 = tkinter.Label(Win,textvariable=TimeStr2,fg='blue',font=("黑体",50))
LabelTime2.pack()
GetTime2()
3.1.2 Text box

The common methods of text boxes are as follows:

method Features
delete(start position, [end position]) Delete the text in the specified area
get(start position, [end position]) Get the text in the specified area
insert(location, [string]...) Insert text into the specified position
see(location) Whether the text is visible at the specified position, returns a boolean
index (mark) Return the row and column where the marker is located
mark_names() Return all tag names
mark_set(mark, position) Return all tag names
mark_unset(mark) Remove mark
  • The value of the position in the above table can be an integer, a floating point number or END (end), for example, 0.0 means column 0 and row 0
  • An example is as follows: Get the current date and time every 1 second and write it into the text box, as follows: In this example, call datetime.now() to get the current date and time, and use the insert() method to retrieve the txt from the text box each time The end (END) begins to append text.
#coding=utf-8
import tkinter
import datetime

def GetTime():
       TimeStr = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))+'\n'
       txt.insert(tkinter.END,TimeStr)
       Win.after(1000,GetTime)  # 每隔1s调用函数 gettime 自身获取时间

Win = tkinter.Tk()
Win.geometry('320x240')
txt = tkinter.Text(Win)
txt.pack()
GetTime()
Win.mainloop()

Insert picture description here

3.1.3 Entry

It is usually used as a control with a relatively single function to receive a single line of text input. Although there are many methods to manipulate the text, the only ones that are usually used are the value method get() and the one used to delete text.delete (start position, end position), For example: Clear the input box asdelete(0,END)

3.2 Button

It is mainly set in response to the mouse click event to trigger the running program, so in addition to the common attributes of the control, the attribute command is the most important attribute. Usually, the program to be triggered by the button is pre-defined in the form of a function, and then the function can be called in the following two ways. The state of the Button button is:'normal','active','disabled'

○ Call the function directly. The parameter expression is "command=function name", pay attention to do not add parentheses after the function name, nor can you pass parameters. For example, the followingcommand=run1:
○ Use anonymous functions to call functions and pass parameters. The expression of the parameter is "command=lambda": Function name (parameter list). For example, the following:"command=lambda:run2(inp1.get(),inp2.get())”。

○ Look at the following example: 1. The input text from the two input boxes is converted to a floating point value for addition, and the result generated by each click of the button is required to be appended to the text box in the form of text, and the original input The box is emptied. 2. Button method one is implemented by calling function run1() without passing parameters, and button "method two" is implemented by calling function run2(x,y) with lambda and passing parameters.

3.3 Radio buttons

3.4 Checkbox

3.5 List box and combo box

3.5.1 List box
3.5.2 Combo box

3.6 Slider)

3.7 Menu

3.8 Child window

3.9 Modal dialog box (Modal)

3.9.1 Interactive dialog
3.9.2 File selection dialog
3.9.3 Color selection dialog

Fourth, incident response

Five, background picture

6. Turn on the camera and display

1. Basic understanding of graphical interface design

Guess you like

Origin blog.csdn.net/zhuan_long/article/details/110918425