我正在尝试创建一个按钮来取消选择两个列表中的所有复选框,但我没有成功 这是我的代码
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_label = tk.Label(LabelFrame, text='')
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"] `my first list`
values = ["Shein", "Pretty\n Little Thing","Mangnopop","Ru21","forever21","blunotes","Streetwearsociety","Saints","Charllote Russe","Fashionnova","Capella",
"lulus","Missguide"] `my second list`
# create a combobox
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)
# create a list of BooleanVar objects to hold the state of the Checkbuttons
checkbuttons_vars = [tk.BooleanVar() for value in values]
checkbuttons_vars1 = [tk.BooleanVar() for value in values1]
# create a Checkbutton widget for each value in the dropdown list
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))
`i have tried adding this deselect() function to deselect the checkbuttons but its still not working`
def deselect():
for widget in window.winfo_children():
if isinstance(Widget,tk.Checkbutton):
Widget.deselect()
deselect = tk.Button(ButtonFrame, text="deselect all", anchor="nw",command=deselect())
deselect.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)
for widget in [some_button]:
widget.pack(expand=True, fill='x', anchor='s',ipady=10)
window.mainloop()
正在创建一个基于计算机的系统,允许用户从复选框中选择各种项目并更新单独组合框中的每个项目我希望用户能够使用取消选择按钮一键取消选择项目 任何想法将不胜感激
至少部分问题在于这段代码:
for widget in window.winfo_children():
if isinstance(Widget,tk.Checkbutton):
Widget.deselect()
您正在使用
widget
(小写“w”)迭代小部件列表,然后在循环内的条件中比较 Widget
(大写“W”)。 Widget
永远不会成为复选按钮,因为它是从 tkinter 导入的类,而不是实际的小部件。
如果您使用
import tkinter as tk
的最佳实践导入了 tkinter - 并且仅该方法 - python 会警告您 Widget
未定义。但是,由于您还执行了
from tkinter import *
,因此
Widget
会被导入。由于您将检查按钮保存在列表中,因此如果您的目标是重置所有检查按钮,则迭代
checkbuttons
和
checkbuttons1
似乎更有意义。