【问题标题】:Clicking on one checkbox activates all of them for some reason由于某种原因,单击一个复选框会激活所有复选框
【发布时间】:2020-12-14 15:21:55
【问题描述】:

我在 Python 中开发 UI 的经验很少,现在使用 tkinter 我遇到了一个问题,我有许多复选框,它们都分配给不同的变量,但出于某种原因单击其中一个复选框会激活所有复选框。

所以在__init__ 中我声明了应该响应单击复选框的变量:

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

然后我创建框架和两组复选框 - 三个复选框应该进入 mid_left 框架,其余的 - 进入 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)

现在所有复选框(以及我为简洁起见未包含的其他 UI 元素)都按预期创建,但现在由于某种原因,当我单击其中一个复选框时,它们都会被激活。这包括整个第二组,默认情况下全部禁用。

有谁知道我在这里做错了什么?

【问题讨论】:

    标签: python tkinter checkbox


    【解决方案1】:

    您需要将复选框“变量”(self.minerology 等)初始化为 tk.IntVar,或者,在您的情况下可能更合适的是 tk.BooleanVar 对象,而不是普通的 Python 整数,例如 0

    【讨论】:

    • 哦,太棒了!谢谢,伙计!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    相关资源
    最近更新 更多