(5) The button combo box control of python tkinter GUI simplification programming

The creation is not easy, please forgive me for your support, thank you!


Python tkinter GUI simplified programming article directory (click to send)

Python tkinter GUI simplified programming article directory


1. Self-introduction, packaging and usage

This series of articles will first explain the tkinter library that encapsulates python, and then will encapsulate other python GUI libraries. The details can be browsed from the first chapter, and will not be explained in the follow-up.


Two, encapsulate the button combo box control

In the first chapter python-GUI简单化编程之基础窗口, we already import loaded package tkinter and use of other libraries, and add the 封装基础窗口, 封装顶层窗口, 数据组合框控件, 调整数据组合框控件code. Now, we are PythonGui.pywadding the following code package 按钮组合框控件, this control currently has one type, and more types of button controls will be updated in the future. Next, I will explain how to use it, and be careful not to delete the previous code.

def B_1创建按钮组合框控件(放置属性,按钮属性,命令函数,参数列表):
    '''
    函数说明:\n\n
    放置属性为一个列表,元素的意义分别为: 0.放置的窗口 1.放置于窗口的行 2.放置于窗口的列\n\n
    按钮属性为一个列表,元素的意义分别为: 0.按钮名称 1.按钮宽度 2.字体大小 3.背景颜色\n\n
    命令函数为函数名,用于调用外部函数\n\n
    参数列表为命令函数的参数\n\n
    '''
    放置窗体 = 放置属性[0]
    放置位置 = 放置属性[1:]
    按钮名称 = 按钮属性[0]
    按钮宽度 = 按钮属性[1]
    字体大小 = 按钮属性[2]
    背景颜色 = 按钮属性[3]
    子窗口 = tk.Frame(放置窗体,height = 50,width = 200 )
    子窗口.grid(row = 放置位置[0],column = 放置位置[1],padx = 2,pady= 2)
    按钮 = tk.Button(子窗口,text = 按钮名称,font=('黑体', 字体大小),width = 按钮宽度,command = lambda:命令函数(参数列表),bg =背景颜色 )
    按钮.grid(row = 0,column = 0,padx = 1,pady= 1)
    return 按钮

Third, the use of package libraries

Add the following code to test.pyw to prepare for testing:

import PythonGui as GUI

def 函数测试(参数列表):
    print(参数列表[0])

主窗体 = GUI.A_建立根页面(['测试软件',400,210,'Beige'],'')
按钮1 = GUI.B_1创建按钮组合框控件([主窗体,1,0],['按钮1',20,12,'orange'],函数测试,['按钮1被按下'])
按钮2 = GUI.B_1创建按钮组合框控件([主窗体,2,0],['按钮2',20,12,'green'],函数测试,['按钮2被按下'])
按钮3 = GUI.B_1创建按钮组合框控件([主窗体,3,0],['按钮3',20,12,'red'],函数测试,['按钮3被按下'])
按钮4 = GUI.B_1创建按钮组合框控件([主窗体,4,0],['按钮4',20,12,'lightgreen'],函数测试,['按钮4被按下'])
主窗体.mainloop()

B_1创建按钮组合框控件()There are four formal parameters in the function: 0. Placement property 1. Button property 2. Command function 3. Parameter list
(1) Placement property is a list, and the meaning of the elements are: 0. Placed window 1. Placed on the window Row 2. Placed in the column of the window
(2) The button attribute attribute is a list, and the meaning of the elements are: 0. Button name 1. Button width 2. Font size 3. Background color.
(3) The command function is the function name, which is actually the name of the external function called. Check the code for details.
(4) The parameter list is a list, which is the parameter of the command function. When using parameters in external functions, please pay attention to the principles in the above code.

Run the above code to generate 4 button combo box controls on the main window. Take button 1 as an example. The button is placed in 主窗体the 1row and 0column. The name of the 按钮1button is 20, the width of the button is , the font size is 12, and the background color of the button is 'orange', 函数测试This external function is 参数列表called, the formal parameter of the external function is , and the actual parameter when calling is'按钮1被按下'

Insert picture description here
Click the buttons respectively, and the following information will be printed:

Insert picture description here


Four, summary

If you have any suggestions, please point it out in the comment area and make progress together, thank you. ps: I usually work a lot of overtime, so I will take the time to update the following chapters.

Guess you like

Origin blog.csdn.net/baidu_37611158/article/details/114946427