lundi 14 décembre 2020

Clicking on one checkbox activates all of them for some reason

I have little experience in developing UI in Python and right now working with tkinter I ran into an issue where I have a number of checkboxes, which are all assigned to different variables, yet for some reason clicking on one of the checkboxes activates all of them.

So in the __init__ I declare variables that should respond to clicking on checkbox:

class ImputerGUI:

    def __init__(self):

        self.mineralogy: int = 0
        self.porosity: int = 0
        self.water_table: int = 0
        self.hem = self.goe = self.vgoe = self.ogoe = self.kao = self.asmc = self.fsmc = \
            self.wmic = self.gibs = self.carb = self.qtz = self.pyr = self.sid = 0


        self.root = tk.Tk()
        self._create_interface()

        self.root.mainloop()

Then later on I create frames and two sets of checkboxes - three checkboxes should go to the mid_left frame and the rest - into the mid_right:

    def _create_interface(self):
        self.root.geometry("800x700")
        self._create_frames()
        self._create_major_component_checkboxes()
        self._create_mineral_checkbuttons()

    def _create_frames(self):
        self.top = tk.Frame(self.root)
        self.mid_left = tk.Frame(self.root, borderwidth=1, relief='sunken')
        self.mid_right = tk.Frame(self.root, borderwidth=1, relief='sunken')
        self.bottom = tk.Frame(self.root)

        self.top.grid(row=0, columnspan=5)
        self.mid_left.grid(row=2, column=0, pady=40)
        self.mid_right.grid(row=2, column=3, pady=40)
        self.bottom.grid(row=3, pady=80, columnspan=5)

    def _create_major_component_checkboxes(self):
        miner_check = tk.Checkbutton(self.mid_left, text='Populate mineralogy variables',
                                     variable=self.mineralogy, justify=tk.LEFT,
                                     onvalue=1, offvalue=0)
        porosity_check = tk.Checkbutton(self.mid_left, text='Populate porosity data',
                                        variable=self.porosity, justify=tk.LEFT,
                                        onvalue=1, offvalue=0)
        wtb_check = tk.Checkbutton(self.mid_left, text='Populate distance from water table',
                                   variable=self.water_table, justify=tk.LEFT,
                                   onvalue=1, offvalue=0)

        miner_check.grid(row=1, column=0)
        porosity_check.grid(row=2, column=0)
        wtb_check.grid(row=3, column=0)

    def _create_mineral_checkbuttons(self):
        miner_dict = {'HEM_R': ['hematite', self.hem],
                      'GOE_R': ['goethite', self.goe],
                      'VGOE_R': ['vitreous_goethite', self.vgoe],
                      'OGOE_R': ['ochreous_goethite', self.ogoe],
                      'KAO_R': ['kaolinite', self.kao],
                      'ASMC_R': ['asmectite', self.asmc],
                      'FSMC_R': ['fsmectite', self.fsmc],
                      'WMIC_R': ['white_mica', self.wmic],
                      'GIBS_R': ['gibbsite', self.gibs],
                      'CARB_R': ['carbonate', self.carb],
                      'QTZ_R': ['quartz', self.qtz],
                      'SID_R': ['siderite', self.sid],
                      'PYR_R': ['pyrite', self.pyr]}

        for key, value, idx in zip(miner_dict.keys(), miner_dict.values(), enumerate(miner_dict)):
            temp = tk.Checkbutton(self.mid_right, text=value[0].replace('_', ' '), variable=value[1],
                               onvalue=1, offvalue=0, state=tk.DISABLED, justify=tk.LEFT)
            temp.grid(row=idx[0], column=4)
            miner_dict[key].append(temp)

Now all checkboxes (and other UI elements that I didn't include for brevity) are created as expected, but now for some reason when I click on one of the checkboxes, all of them get activated. And that includes the entire second group which are all disabled by default.

Does anyone know what I'm doing wrong here?




Aucun commentaire:

Enregistrer un commentaire