python 2.7 使用 tkinter -all 复选框仅在单击一个时被选中 [英] python 2.7 using tkinter -all checkbox are being checked when click on one only

查看:66
本文介绍了python 2.7 使用 tkinter -all 复选框仅在单击一个时被选中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Tkinter 中使用 python 2.7(Tkinter 的新手:))我有一个包含 20 个复选框的用户界面一旦我点击一个复选框,所有复选框都被选中,而不是一个.在下面的代码中,您将看到 2 行(用 # 一次)with # - 当点击复选框时,只选中一个,这是可以的没有 # -当点击一个时,所有都被检查问题是我想知道每个复选框的状态是否被选中,我必须定义 var=Intvar 才能获取"它的状态有什么建议吗?提前致谢以下是相关定义

I'm using python 2.7 with Tkinter (new to Tkinter:)) I have UI with list of 20 checkboxes once I click on one checkbox, all checkboxes are being checked, instead of one. In code below you'll see 2 line (once with #) with # -when click on checkbox only one is checked which is ok without # -when click on one, all are being checked The problem is that I want to know the status of each checkbox if checed or not and I have to define var=Intvar in order to "get" it status Any suggestion? Thanks in advance Below is relevant def

def suites_checkbox_create(self):
    ExcelWorkBook1 = open_workbook(config.UI_Suites_Location + 'STD_SUITES.xlsx', on_demand=True)
    First_Sheet1 = ExcelWorkBook1.sheet_by_index(0)
    plushight = 110
    suitesList=[]
    self.CheckboxList=[]
    for name in (First_Sheet1._cell_values):
        if name[3] == "General Name":
            continue
        else:
            suitesList.append(name[3])


    for index, name in enumerate(suitesList):

            self.var=IntVar
            #self.CheckboxList.append(Checkbutton(self.app, text=name)) # using this, i can check once checkbox a time
            self.CheckboxList.append(Checkbutton(self.app, text=name, variable=self.var)) # with this, once i check once checkbox, all checkboxes(20) are bing checked
            self.CheckboxList[index].place(y=plushight)
            plushight += 20

推荐答案

发生这种情况的原因是因为您为所有 Checkbutton 小部件的 变量提供了相同的变量 属性.

The reason this happens is because you've given all of your Checkbutton widgets the same variable for their variable attribute.

意味着一旦 Checkbutton 小部件之一被勾选 self.var 被赋予 1 的值,这意味着所有Checkbutton 小部件的值为 1,这相当于它们已被选中.

Meaning that as soon as one of the Checkbutton widgets is ticked self.var is given a value of 1 which means that all of the Checkbutton widgets have a value of 1 which equates to them having been selected.

简而言之,每当一个被勾选时,它都会更新所有其他人的值,因为它们具有用于存储其值的相同变量.

In short, whenever one is ticked it updates the value of all the other's because they have the same variable used to store their value.

在下面的例子中看到这一点:

See this in the example below:

from tkinter import *

root = Tk()
var = IntVar()

for i in range(10):
    Checkbutton(root, text="Option "+str(i), variable = var).pack()

root.mainloop()

要解决此问题,您需要为每个 Checkbutton 使用不同的变量,如下所示:

To resolve this you need to use a different variable for each Checkbutton, like the below:

from tkinter import *

root = Tk()
var = []

for i in range(10):
    var.append(IntVar())
    Checkbutton(root, text="Option "+str(i), variable = var[i]).pack()

root.mainloop()

这篇关于python 2.7 使用 tkinter -all 复选框仅在单击一个时被选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆