Tkinter里面有一个button事件

Button ( master, option=value, ... )

Button(根对象, [属性列表])

根对象:在那个窗体显示,例如主窗体。

属性列表:是可选的属性=属性值组成。

属性 说明
text 标签显示的文本
font 设置文本的字体和大小
fg(foreground) 字体的颜色,
bg (background) 标签的背景色
width 标签的宽度(一个中文的字体宽为单位)
height 标签的高度(一个中文的字体高为单位)
cursor 鼠标的样式
command 绑定事件
padx 文字到边框的距离,水平方向
pady 文字到边框的距离,垂直方向
bd(borderwidth) 边框的宽度
relief 边框的样式
justify 文本对齐方式
image 图片
compound 图片与文字的混搭
anchor 方位

在这么多属性里有一个command属性最特殊。本来它是绑定一个你自己写的函数的,但是这个函数必须不能带返回值,否则它command就会自动运行,根本等不到你去按这个按钮,但是有时候我们又希望有一个返回值使用那么就必须在里面使用lambda函数做间隔,如

def get_category():
    def make_base():
               
        a.append(1)
        root.quit()
    def make():
        
        a.append(2)
        root.quit()
        
    root = tkinter.Tk()
    root.wm_attributes('-topmost', 1)
    screenwidth, screenheight = root.maxsize()
    width = 150
    height = 100
    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
    root.geometry(size)
    root.resizable(0, 0) 
    

    a=[]
    B1= tkinter.Button(root, text ="制作数据库",bd = 8 ,command=lambda : make_base())
    B2 = tkinter.Button(root, text ="制作日常统计",bd = 8 ,command=lambda:make())
     
    B1.pack()
    B2.pack()
    root.mainloop()
    root.destroy()
    return a

在这个函数里command就用来lambda来做间隔完成了值的传递

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐