TKinter CheckButtons无法正确更新值

时间:2019-07-15 16:55:30

标签: python user-interface tkinter checkbox

我知道这个问题已经问过几次了,但是没有其他解决方案可以解决我的问题。

我有一个存储为元组的“异常”的可变列表,其中包含异常名称,并为0或1,确定是否通知用户某些信息。由于列表的长度可变,因此需要在for循环中创建检查按钮。

我想创建一个显示检查按钮列表的弹出窗口,以允许用户根据自己的喜好编辑通知值。但是,我使用的这种想法的实现导致检查按钮不会更改其变量的值或显示适当的开/关状态。

这是我的代码:

notif_anoms = [("Anomaly 1", 1), ("Anomaly 2", 0)]
checkbox_vars = []

    def select_desired_anomaly_checks(self):
        popup = Tk()
        popup.wm_title("Desired Anomalies")
        len_a = len(self.notif_anoms)

        for i in range(0, len_a):
            msg, on = self.notif_anoms[i]

            self.checkbox_vars.append(IntVar(value=on))
            self.checkbox_vars[-1].set(on)

            tk.Checkbutton(popup, text=msg, variable=self.checkbox_vars[-1], onvalue=1, offvalue=0, command=self.update_vars).grid(row=i, sticky=W)
        popup.resizable(0, 0)
        popup.mainloop()

    def update_vars(self):
        for i in range(0, len(self.checkbox_vars)): 
            var = self.checkbox_vars[i]
            print(var.get())
            self.notif_anoms[i] = (self.notif_anoms[i][0], var.get())
        print('------------------')

我能想到的唯一问题是我在for循环中设置了IntVar,但据我所知,没有其他方法可以在运行时创建长度未知的复选框列表。

感谢您的大力支持。

0 个答案:

没有答案
相关问题