• Tkinter 之CheckButton复选框标签


    一、参数说明

    语法作用
    Checkbutton(root,text='xxxx') 复选框显示的文本
    Checkbutton(root,variable=id) 通过变量的值确定哪些复选框被选中
    Checkbutton(root,variable=id,onvalue=1) 复选框选中(有效)时变量的值
    Checkbutton(root,variable=id,onvalue=1,offvalue=0) 复选框未选中(无效)时变量的值
    Checkbutton(root,variable=id,onvalue=1,offvalue=0,command=函数) 复选框选中时执行的命令(函数)

    二、代码示例

    import tkinter as tk
    
    window = tk.Tk()
    
    def main():
        global window
        window.title("CheckButton参数说明")
        winWidth = 600
        winHeight = 400
        # 获取屏幕宽高
        screenWidth = window.winfo_screenwidth()
        screenHeight = window.winfo_screenheight()
        
        x = int((screenWidth - winWidth) / 2)
        y = int((screenHeight - winHeight) / 2)
        # 设置窗口居中
        window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))
        window.iconbitmap("./image/icon.ico")
        
        """checkbutton参数.
    
            Valid resource names: activebackground, activeforeground, anchor,
            background, bd, bg, bitmap, borderwidth, command, cursor,
            disabledforeground, fg, font, foreground, height,
            highlightbackground, highlightcolor, highlightthickness, image,
            indicatoron, justify, offvalue, onvalue, padx, pady, relief,
            selectcolor, selectimage, state, takefocus, text, textvariable,
            underline, variable, width, wraplength."""
        var1 = tk.IntVar()
        var2 = tk.IntVar()
        tk.Checkbutton(window, text="篮球", onvalue=1, offvalue=0,
                       variable=var1, command=callBack, activebackground="#f00",
                       foreground="#666").pack()
        tk.Checkbutton(window, text="足球", onvalue=1, offvalue=0,
                       variable=var2, command=callBack, activebackground="#f00",
                       foreground="#bbb").pack()
        window.mainloop()
        
    def callBack():
        print(1)
    if __name__ == '__main__':
        main()
    

      

    三、效果图

  • 相关阅读:
    实验四 决策树算法及应用
    实验三 朴素贝叶斯算法及应用
    实验二 K-近邻算法及应用
    实验一 感知器及其应用
    实验三 面向对象分析与设计
    实验二 结构化分析与设计
    实验一 软件开发文档与工具的安装与使用
    ATM管理系统
    活动图与流程图的区别与联系
    四则运算题目生成程序
  • 原文地址:https://www.cnblogs.com/yang-2018/p/11784892.html
Copyright © 2020-2023  润新知