zoukankan      html  css  js  c++  java
  • Tkinter(六):Checkbutton 复选按钮

     效果:

    选择不同的复选框,会显示选中的内容在顶部label上

    import tkinter as tk
    
    # 定义窗口
    window = tk.Tk()
    window.title('my window')  # 窗口title
    window.geometry('350x300')  # 窗口尺寸
    
    # 定义Label
    l = tk.Label(window, bg="yellow", width=20, text='you have selected None')
    l.pack()
    
    
    def print_selection():
        if (var1.get() == 1 and var2.get() == 0):
            l.config(text='I love only Python')
        elif (var1.get() == 0 and var2.get() == 1):
            l.config(text='I love only C++')
        elif (var1.get() == 1 and var2.get() == 1):
            l.config(text='I love both')
        else:
            l.config(text='I do not love either')
    
    
    var1 = tk.IntVar()
    var2 = tk.IntVar()
    '''
    state状态:active, disabled,normal
    onvalue:复选按钮选中时变量的值
    offvalue:复选按钮未选中时变量的值
    variable:通过变量的值确定哪些复选按钮被选中,每个复选按钮使用不同的变量,复选按钮之间相互独立
    '''
    c1 = tk.Checkbutton(window, text='Python', variable=var1, onvalue=1, offvalue=0, command=print_selection)
    c2 = tk.Checkbutton(window, text='C++', variable=var2, onvalue=1, offvalue=0, command=print_selection)
    
    c1.pack()
    c2.pack()
    
    window.mainloop()

    总结:

    1.复选框被勾选上时,variable = var1 = onvalue =1

    复选框没有被勾上时,variable = var1 = offvalue =0

    2.获取值使用var1.get()

    3.条件判断语句,python2和python3中略有不同,python3中使用and or not

    4.使用state参数可以表明复选框的状态,参数有active,disabled,normal

    我走的很慢,但从不后退
  • 相关阅读:
    我再也不相信正解了……
    悬线法
    防线 Defence
    奶牛浴场
    环状两段最大子段和
    三步必杀
    加工生产调度
    种树
    UVA11134 传说中的车 Fabled Rooks
    UVA 11054 Gergovia的酒交易 Wine trading in Gergovia
  • 原文地址:https://www.cnblogs.com/ronyjay/p/13706541.html
Copyright © 2011-2022 走看看