Tkinter 复选框实现全选/取消功能

通过对全选复选框做鼠标事件绑定,进行全选和取消全选操作

点击按钮可以输出勾选的值


from tkinter import *
from tkinter.ttk import *

select_category = []
def confirm():
    # 选择勾选的select_category
    select_category = [i.get() for i in v if i.get()]
    if 'All' in select_category:
        select_category.remove('All')
    print(select_category)


def allconfirm(event):
    if v[-1].get():
        for index, item in enumerate(category):
            v[index].set('')
    else:
        for index, item in enumerate(category):
            v[index].set(item)


window = Tk()
window.title("hello world")
menu = Menu(window, tearoff=False)
# category
v = []
Label(window, text="请选取").pack(padx=5, pady=10)
frame5 = Frame(window)
frame5.pack(side="top")
category = ["A", "B", "C", "D", "E"]


for index, item in enumerate(category):
    v.append(StringVar())
    Checkbutton(frame5, text=item, variable=v[-1], onvalue=item, offvalue='').grid(row=index+1, sticky='w')
v.append(StringVar())
all = Checkbutton(frame5, text='All', variable=v[-1], onvalue='All', offvalue='')
all.grid(row=0, sticky='w')

# Button
frame6 = Frame(window)
frame6.pack(side="top")
Button(frame6, text="生成", command=confirm).pack(padx=5, pady=10)


all.bind("

 

Tkinter 复选框实现全选/取消功能_第1张图片Tkinter 复选框实现全选/取消功能_第2张图片

 

 

你可能感兴趣的:(python,开发语言)