Skip to content
Advertisement

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

Is there any way to create all these Checkbuttons “grp_material_1”, “grp_material_2″,”grp_material_…” by using a loop ?

I try but got an error that the name (“grp_material_1”, “grp_material_2″…) can not be a variable. I would like to reduce my code of course for a better readability. Thank you !

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")

Advertisement

Answer

The simplest thing would be to add all these to a dict and reference them from there:

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")
}

Then you’d reference them like cadre_choice_material[1] or grp_material[12].

You could make the code even more concise (though arguably less readable) with creative use of generators:

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")
    ]
}

The two code snippets above should give the exact same results, but the first one is longer & more readable, the second is shorter & more extensible.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement