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

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐