如何为tkinter复选框小部件创建全选按钮



我从上一个问题中复制了代码,并试图修改它以使用框架和网格管理器,但我似乎无法使其工作。我认为问题可能出在create_cbuts函数和对frame1的引用中。任何帮助都将不胜感激!

from tkinter import *           # Python interface to the Tk GUI toolkit
from tkinter import ttk         # Tk themed widget set
from tkinter import messagebox
def donothing():
return
def create_cbuts():
for index, item in enumerate(cbuts_text):
cbuts.append(Checkbutton(frame1, text = item))
cbuts[index].grid
def select_all():
for i in cbuts:
i.select()
def deselect_all():
for i in cbuts:
i.deselect()
def process_checkbox():
if (check_an.get() == 1):
print('Ancestry button checked')
if (check_ft.get() == 1):
print('FTDNA button checked')
if (check_gm.get() == 1):
print('GEDmatch button checked')
if (check_mh.get() == 1):
print('MyHeritage button checked')
if (check_me.get() == 1):
print('23&Me button checked')
root = Tk()
win2 = Toplevel(root)
win2.geometry('500x150')    # Width x Height
win2.resizable(0, 0)        # prevent window resizing
win2.title('Process DNA Match Files')
frame1 = Frame(win2)
frame2 = Frame(win2)
Label(frame1, text="Which files should be processed?").grid(row=0, column=0, sticky=W)
check_an = IntVar()
Checkbutton(frame1, text="Ancestry", variable=check_an).grid(row=1,column=0, sticky=W)
check_ft = IntVar()
Checkbutton(frame1, text="FTDNA", variable=check_ft).grid(row=2, column=0, sticky=W)
check_gm = IntVar()
Checkbutton(frame1, text="GEDmatch", variable=check_gm).grid(row=3, column=0, sticky=W)
check_mh = IntVar()
Checkbutton(frame1, text="MyHeritage", variable=check_mh).grid(row=1, column=1, sticky=W)
check_me = IntVar()
Checkbutton(frame1, text="23 and Me", variable=check_me).grid(row=2, column=1, sticky=W)
frame1.pack()
select_button = ttk.Button(frame2, text='Select All', width=15, command=select_all)
select_button.pack(side = LEFT, padx=5, pady=5)
select_button = ttk.Button(frame2, text='Deselect All', width=15, command=deselect_all)
select_button.pack(side = LEFT, padx=5, pady=5)
cancel_button =ttk.Button(frame2, text='Cancel', width=15, command=win2.destroy)
cancel_button.pack(side = LEFT, padx=5, pady=5)
ok_button =ttk.Button(frame2, text='OK', width=15, command=process_checkbox)
ok_button.pack(side = LEFT, padx=5, pady=5)
frame2.pack()
cbuts_text = ['Ancestry','FTDNA','GEDmatch','MyHeritage', '23 and Me']
cbuts = []
create_cbuts()
root.mainloop()
'''''

谢谢,Bryan。你为我指明了正确的方向。我删除了create_cbuts函数,并修改了select_all和deselect_all函数,如下所示。不那么优雅,但它有效!

def select_all():
check_an.set(True)
check_ft.set(True)
check_gm.set(True)
check_mh.set(True)
check_me.set(True)
def deselect_all():
check_an.set(False)
check_ft.set(False)
check_gm.set(False)
check_mh.set(False)
check_me.set(False)

最新更新