希望您能帮助我解决这个问题。我需要在复选框中包含“全选”。一切正常,直到我创建了另一组复选框。通过单击一个复选框组中的“全选”,还将选中另一个复选框组中的“全选”。
到目前为止,这是我的代码:
#first checkbox group
G1_SUBFRAME = Frame(ENTRY_FRAME)
G1_SUBFRAME.grid(row=1, column=0, padx=10, pady=10)
Label(G1_SUBFRAME, text= "WORKTYPES",
fg='black', font='none 10 bold', borderwidth=5).grid(row=0, column=0, columnspan=20)
G1_A = IntVar()
G1_B = IntVar()
G1_A_BOX = Checkbutton(G1_SUBFRAME, text = "G1_A", variable = G1_A, \
onvalue = 1, offvalue = 0, height=1, \
width = 15, anchor=W, font = "None 10 bold")
G1_B_BOX = Checkbutton(G1_SUBFRAME, text = "G1_B", variable = G1_B, \
onvalue = 1, offvalue = 0, height=1, \
width = 15, anchor=W, font = "None 10 bold")
G1_ALL = [G1_A_BOX, G1_B_BOX]
def G1_checkall():
for element in G1_ALL:
element.select()
G1_ALL_BOX = Checkbutton(G1_SUBFRAME, text = "SELECT ALL", command=G1_checkall, \
onvalue = 1, offvalue = 0, height=1, \
width = 15, anchor=W, font = "None 10 bold")
G1_A_BOX.grid(row = 4, column = 2)
G1_B_BOX.grid(row = 5, column = 2)
G1_ALL_BOX.grid(row = 6, column = 2)
#second checkbox group
G2_SUBFRAME = Frame(ENTRY_FRAME)
G2_SUBFRAME.grid(row=1, column=1, padx=10, pady=10)
Label(G2_SUBFRAME, text= "FILETYPES",
fg='black', font='none 10 bold', borderwidth=5).grid(row=0, column=0, columnspan=20)
G2_A = IntVar()
G2_B = IntVar()
G2_A_BOX = Checkbutton(G2_SUBFRAME, text = "G2_A", variable = G2_A, \
onvalue = 1, offvalue = 0, height=1, \
width = 25, anchor=W, font = "None 10 bold")
G2_B_BOX = Checkbutton(G2_SUBFRAME, text = "G2_B", G2_B, \
onvalue = 1, offvalue = 0, height=1, \
width = 25, anchor=W, font = "None 10 bold")
G2_ALL = [G2_A_BOX, G2_B_BOX]
def G2_checkall():
for element in G2_ALL:
element.select()
G2_ALL_BOX = Checkbutton(G2_SUBFRAME, text = "SELECT ALL", command=G2_checkall, \
onvalue = 1, offvalue = 0, height=1, \
width = 25, anchor=W, font = "None 10 bold")
G2_A_BOX.grid(row = 4, column =2)
G2_B_BOX.grid(row = 5, column =2)
G2_ALL_BOX.grid(row = 6, column = 2)
答案 0 :(得分:2)
在您的问题中,只需将IntVar()分配给两个全选复选框即可。
如果要更改检查状态(如全选按钮),则尝试在相关功能内出现其他情况时应用。
这是解决方案,
import tkinter
from tkinter import *
ENTRY_FRAME = Tk()
#first checkbox group
G1_SUBFRAME = Frame(ENTRY_FRAME)
G1_SUBFRAME.grid(row=1, column=0, padx=10, pady=10)
Label(G1_SUBFRAME, text= "WORKTYPES",
fg='black', font='none 10 bold', borderwidth=5).grid(row=0, column=0, columnspan=20)
G1_A = IntVar()
G1_B = IntVar()
G1_A_BOX = Checkbutton(G1_SUBFRAME, text = "G1_A", variable = G1_A, \
onvalue = 1, offvalue = 0, height=1, \
width = 15, anchor=W, font = "None 10 bold")
G1_B_BOX = Checkbutton(G1_SUBFRAME, text = "G1_B", variable = G1_B, \
onvalue = 1, offvalue = 0, height=1, \
width = 15, anchor=W, font = "None 10 bold")
G1_ALL = [G1_A_BOX, G1_B_BOX]
G1_ALL_VAR = IntVar()
def G1_checkall():
if G1_ALL_VAR.get()==1:
for element in G1_ALL:
element.select()
else:
for element in G1_ALL:
element.deselect()
G1_ALL_BOX = Checkbutton(G1_SUBFRAME, text = "SELECT ALL",variable = G1_ALL_VAR, command=G1_checkall, \
onvalue = 1, offvalue = 0, height=1, \
width = 15, anchor=W, font = "None 10 bold")
G1_A_BOX.grid(row = 4, column = 2)
G1_B_BOX.grid(row = 5, column = 2)
G1_ALL_BOX.grid(row = 6, column = 2)
#second checkbox group
G2_SUBFRAME = Frame(ENTRY_FRAME)
G2_SUBFRAME.grid(row=1, column=1, padx=10, pady=10)
Label(G2_SUBFRAME, text= "FILETYPES",
fg='black', font='none 10 bold', borderwidth=5).grid(row=0, column=0, columnspan=20)
G2_A = IntVar()
G2_B = IntVar()
G2_A_BOX = Checkbutton(G2_SUBFRAME, text = "G2_A", variable = G2_A, \
onvalue = 1, offvalue = 0, height=1, \
width = 25, anchor=W, font = "None 10 bold")
G2_B_BOX = Checkbutton(G2_SUBFRAME, text = "G2_B", variable = G2_B, \
onvalue = 1, offvalue = 0, height=1, \
width = 25, anchor=W, font = "None 10 bold")
G2_ALL = [G2_A_BOX, G2_B_BOX]
G2_ALL_VAR = IntVar()
def G2_checkall():
if G2_ALL_VAR.get()==1:
for element in G2_ALL:
element.select()
else:
for element in G2_ALL:
element.deselect()
G2_ALL_BOX = Checkbutton(G2_SUBFRAME, text = "SELECT ALL",variable = G2_ALL_VAR, command=G2_checkall, \
onvalue = 1, offvalue = 0, height=1, \
width = 25, anchor=W, font = "None 10 bold")
G2_A_BOX.grid(row = 4, column =2)
G2_B_BOX.grid(row = 5, column =2)
G2_ALL_BOX.grid(row = 6, column = 2)