You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Tkinter中实现一键取消所有复选框选中状态(不删除列表)

一键取消所有复选框选中状态的解决方案

问题根源

你的代码有两个核心问题导致取消选中功能失效:

  • 按钮绑定命令时错误使用了deselect(),这会在程序启动时直接执行函数,而非点击按钮时触发
  • 只遍历了window的直接子组件,而复选框嵌套在LabelFrame和LabelFrame1内部,无法被遍历到

修正方案

  1. 修正按钮命令绑定:将command=deselect()改为command=deselect,确保点击按钮时才执行取消选中逻辑
  2. 遍历所有复选框变量:直接操作存储复选框状态的BooleanVar列表,这比遍历控件更可靠,也能覆盖所有嵌套的复选框
  3. 同步更新下拉框:取消选中后同步清空下拉框内容,提升用户体验

修正后的完整代码

import tkinter 
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os
import openpyxl
import tkinter as tk
from tkinter import Button


window = tk.Tk()
window.geometry('2200x820')
MainFrame = tk.Frame(window, width=385, height=460, relief='raised', borderwidth=1)
ButtonFrame = tk.Frame(MainFrame, width=375, height=30, relief='raised', borderwidth=1)
LabelFrame = tk.Frame(MainFrame, width=375, height=1, relief='raised', background="yellow", borderwidth=0)
LabelFrame1 = tk.Frame(MainFrame, width=375, height=1, relief='raised', background="yellow", borderwidth=0.5)


some_button = tk.Button(ButtonFrame, text='Enter data', command=window.destroy)



values1 = ["Shein", "Pretty Little Thing","Mangnopop","Ru21","forever21","blunotes","Streetwearsociety","Saints","Charllote Russe","Fashionnova","Capella",
          "lulus","Missguide"]    


values = ["Shein", "Pretty\n Little Thing","Mangnopop","Ru21","forever21","blunotes","Streetwearsociety","Saints","Charllote Russe","Fashionnova","Capella",
          "lulus","Missguide"]       

combobox = ttk.Combobox(ButtonFrame, width=30)
combobox1 = ttk.Combobox(ButtonFrame, width=30)

combobox.grid(row=1,column=2,padx=10)
combobox1.grid(row=2,column=2)



checkbuttons_vars = [tk.BooleanVar() for value in values]
checkbuttons_vars1 = [tk.BooleanVar() for value in values1]

checkbuttons = []
checkbuttons1 = []


for index, value in enumerate(values):
   checkbutton = tk.Checkbutton(LabelFrame,text=value, variable=checkbuttons_vars[index])
   checkbutton.config(background="yellow", fg="black", font=("Fira Code", 8), 
                   selectcolor="white", relief="flat", padx=1, pady=1)
   checkbutton.pack(side="left",anchor="w",fill="both")
   checkbuttons.append(checkbutton)
   

for index, value in enumerate(values1):
   checkbutton = tk.Checkbutton(LabelFrame1,text=value,offvalue = 0, variable=checkbuttons_vars1[index])
   checkbutton.config(background="yellow", fg="black", font=("Fira Code", 8), 
                   selectcolor="white", relief="flat", padx=1, pady=1)
   checkbutton.pack(side="left",anchor="w",fill="both")
   checkbuttons1.append(checkbutton)



def update_combobox():
   selected_values = [value for value, var in zip(values, checkbuttons_vars,) if var.get()]
   selected_values1 = [value for value, var in zip(values1, checkbuttons_vars1,) if var.get()]
   combobox.configure(width=40, height=7)
   combobox.delete(0, tk.END)
   combobox.insert(0," , " .join(selected_values,))
   combobox.insert(0," , ")
   combobox.insert(0,"  , ".join(selected_values1))
   
       


def update_combobox1():
   selected_values = [value for value, var in zip(values, checkbuttons_vars) if var.get()]
   selected_values1 = [value for value, var in zip(values1, checkbuttons_vars1,) if var.get()]
   combobox1.configure(width=40, height=7)
   combobox1.delete(0, tk.END)
   combobox1.insert(0, ", ".join(selected_values))
   combobox1.insert(0," , ")
   combobox1.insert(0,"  ,  ".join(selected_values1))



def deselect():
    # 重置所有复选框的状态变量
    for var in checkbuttons_vars:
        var.set(False)
    for var in checkbuttons_vars1:
        var.set(False)
    # 同步清空下拉框内容
    combobox.delete(0, tk.END)
    combobox1.delete(0, tk.END)


# 修正按钮绑定,去掉括号
deselect_btn = tk.Button(ButtonFrame, text="deselect all", anchor="nw",command=deselect)
deselect_btn.grid(row=1,column=5)

update_button = tk.Button(ButtonFrame, text="Update", anchor="nw",command=update_combobox)
update_button.grid(row=1,column=3)

update_button1 = tk.Button(ButtonFrame, text="Update", anchor="nw",command=update_combobox1)
update_button1.grid(row=2,column=3)


for frame in [MainFrame, LabelFrame, LabelFrame1, ButtonFrame,]:
    frame.pack(expand=True, fill='both')
    frame.pack_propagate(0)

# 修正拼写错误 widgetin -> widget_in
for widget_in in [some_button]:
    widget_in.pack(expand=True, fill='x', anchor='s',ipady=10)

window.mainloop()

额外修正说明

  • 修复了代码中的拼写错误widgetin改为widget_in,避免运行报错
  • 取消选中后自动清空下拉框,保持界面状态一致

内容的提问来源于stack exchange,提问作者Dennis Wayne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 16:09:56