Python tkinter 如何使用循环创建所有这些 Checkbutton?

Python tkinter How can I created all these Checkbuttons by using a loop?

有没有办法通过循环创建所有这些 Checkbutton“grp_material_1”、“grp_material_2”、“grp_material_...”?

我尝试了,但出现错误,名称 ("grp_material_1", "grp_material_2"...) 不能是变量。我当然想减少我的代码以获得更好的可读性。谢谢!

import os
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox

fenetre_choice_of_mat=Tk()
fenetre_choice_of_mat.geometry("350x400")

choice_material_list=[]

class Checkbuttongroup:
    def __init__(self, fenetre1, text1):
        self.Checkbutton1 = IntVar()
        self.Checkbutton1.set(1)
        self.texte=text1
        self.fenetre12=fenetre1
        Button1 = Checkbutton(self.fenetre12, text = self.texte, 
            variable = self.Checkbutton1,
            onvalue = 1,
            offvalue = 0,
            height = 2,
            width = 20,
            anchor = "w",
            command=self.test)
        Button1.pack()

        choice_material_list.append(self.texte)


    def test(self):
        if self.Checkbutton1.get()==1:
            choice_material_list.append(self.texte)
        else:
            if self.texte in choice_material_list:
                choice_material_list.remove(self.texte)

        print(choice_material_list)

titre_choice_of_mat=Label(fenetre_choice_of_mat,text="Check what should stay in calculation \n Uncheck what should be taken out")
titre_choice_of_mat.pack()
bottom_frame=Frame(fenetre_choice_of_mat)
bottom_frame.pack(side='bottom')

cadre_choice_material_1=Frame(fenetre_choice_of_mat,highlightbackground="blue", highlightthickness=2)
cadre_choice_material_1.pack(side='left')
cadre_choice_material_2=Frame(fenetre_choice_of_mat,highlightbackground="blue", highlightthickness=2)
cadre_choice_material_2.pack(side='left')
grp_material_1=Checkbuttongroup(cadre_choice_material_1,"CW")
grp_material_2=Checkbuttongroup(cadre_choice_material_1,"CS")
grp_material_4=Checkbuttongroup(cadre_choice_material_1,"AE")
grp_material_6=Checkbuttongroup(cadre_choice_material_1,"CF")
grp_material_7=Checkbuttongroup(cadre_choice_material_1,"SE")
grp_material_3=Checkbuttongroup(cadre_choice_material_1,"RCC")
grp_material_5=Checkbuttongroup(cadre_choice_material_1,"CD")
grp_material_8=Checkbuttongroup(cadre_choice_material_2,"FS")
grp_material_9=Checkbuttongroup(cadre_choice_material_2,"COP")
grp_material_11=Checkbuttongroup(cadre_choice_material_2,"CR")
grp_material_12=Checkbuttongroup(cadre_choice_material_2,"AB")
grp_material_13=Checkbuttongroup(cadre_choice_material_2,"CT")
grp_material_14=Checkbuttongroup(cadre_choice_material_2,"LP")
grp_material_10=Checkbuttongroup(cadre_choice_material_2,"SD")


bouton_save=Button(bottom_frame, text="Save",width=30,height=5,bg="white",bd=5,command=fenetre_choice_of_mat.destroy)
bouton_save.pack(anchor=N)





os.system("pause")

最简单的方法是将所有这些添加到 dict 并从那里引用它们:

cadre_choice_material = {
    1: Frame(fenetre_choice_of_mat, highlightbackground="blue", highlightthickness=2),
    2: Frame(fenetre_choice_of_mat, highlightbackground="blue", highlightthickness=2)
}
cadre_choice_material[2].pack(side='left')
grp_material = {
    1: Checkbuttongroup(cadre_choice_material[1], "CW"),
    2: Checkbuttongroup(cadre_choice_material[1], "CS"),
    4: Checkbuttongroup(cadre_choice_material[1], "AE"),
    6: Checkbuttongroup(cadre_choice_material[1], "CF"),
    7: Checkbuttongroup(cadre_choice_material[1], "SE"),
    3: Checkbuttongroup(cadre_choice_material[1], "RCC"),
    5: Checkbuttongroup(cadre_choice_material[1], "CD"),
    8: Checkbuttongroup(cadre_choice_material[2], "FS"),
    9: Checkbuttongroup(cadre_choice_material[2], "COP"),
    11: Checkbuttongroup(cadre_choice_material[2], "CR"),
    12: Checkbuttongroup(cadre_choice_material[2], "AB"),
    13: Checkbuttongroup(cadre_choice_material[2], "CT"),
    14: Checkbuttongroup(cadre_choice_material[2], "LP"),
    10: Checkbuttongroup(cadre_choice_material[2], "SD")
}

然后您可以像 cadre_choice_material[1]grp_material[12] 那样引用它们。

您可以通过创造性地使用生成器使代码更加简洁(尽管可读性可能会降低):

cadre_choice_material = {
    i: Frame(fenetre_choice_of_mat, highlightbackground="blue", highlightthickness=2)
    for i in (1, 2)
}
cadre_choice_material[2].pack(side='left')
grp_material = {
    i: Checkbuttongroup(cadre_choice_material[j], s) for i, j, s in [
        (1, 1, "CW"), (2, 1, "CS"), (4, 1, "AE"), (6, 1, "CF"), (7, 1, "SE"),
        (3, 1, "RCC"), (5, 1, "CD"), (8, 2, "FS"), (9, 2, "COP"), (11, 2, "CR"),
        (12, 2, "AB"), (13, 2, "CT"), (14, 2, "LP"), (10, 2, "SD")
    ]
}

上面的两个代码片段应该给出完全相同的结果,但第一个更长且更具可读性,第二个更短且更可扩展。