Tkinter Checkbutton:获取多个对象的变量
Tkinter Checkbutton: getting the variable for multiple objects
我希望它能即时创建一堆复选框,并在按下 'Submit' 按钮时查找所有选中的复选框。目前,当按 'Submit' 调用 select_adgroup()
时,它会为每个项目打印 0
,无论它们是否被选中,除非每个框都被选中,在这种情况下它会打印 1
对于每个项目。但我希望它只为选中的框打印 1
。
def search_adgroups(self):
self.adgroups = adgroup(checkBoxVal.get())
self.inc1 = 1
self.cbuts1 = []
for index, item in enumerate(self.adgroups):
self.adBoxVal = IntVar()
self.adgroup_check = (Checkbutton(self, variable=self.adBoxVal, text = item))
self.cbuts1.append(self.adgroup_check)
self.cbuts1[index].grid(row=self.inc1, sticky=W)
self.inc1 += 1
self.button2 = Button(self, text="Submit", command=self.select_adgroup)
self.button2.grid(row=self.inc1, sticky=W)
def select_adgroup(self):
for item in self.cbuts1:
print(self.adBoxVal.get())
首先它不是if all checked
,而是if last one checked
它会打印1s。所以总的来说它只打印最后一项的价值。这说明了很多问题。
在for
循环之后,self.adBoxVal
的值将是最后一个,所以当你试图在你的方法中用self.adBoxVal.get()
获取它的值时,你只会得到最后一个价值。
要解决这个问题,您需要将所有 self.adBoxVal
存储在一个列表中,然后对其进行迭代。
def search_adgroups(self):
...
...
self.chks = [] #your list for IntVars
for index, item in enumerate(self.adgroups):
adBoxVal = IntVar() #no need self here as BryanOakley points out
self.chks.append(self.adBoxVal)
#no need self at below also
adgroup_check = Checkbutton(self.root, variable=adBoxVal, text = item)
self.cbuts1.append(adgroup_check)
self.cbuts1[index].grid(row=self.inc1, sticky=W)
self.inc1 += 1
self.button2 = Button(self.root, text="Submit", command=self.select_adgroup)
self.button2.grid(row=self.inc1, sticky=W)
def select_adgroup(self):
for item in self.chks: #here you need to iterate over IntVars
#to get thier value
print (item.get())
此外,对于您以后的问题,如果您 post 一个 有效的 代码可以重现没有一些未知函数或变量的错误,那就太好了。
编辑: 让我们用 print
来说明。当您在 self.adBoxVal = IntVar()
下添加 print(self.addBoxVal)
时,您会看到每个项目都不同。
for index, item in enumerate(self.adgroups):
adBoxVal = IntVar()
print (adBoxVal)
>>>
PY_VAR0 #I assigned range(5) to adgroups that's why
PY_VAR1 #there are 5 elements here
PY_VAR2
PY_VAR3
PY_VAR4
如果您在 您的 代码中的 select_adgroup
方法中添加 print (adBoxVal)
,您将看到它是 PY_VARX
(最后一个)和代码仅适用于 with/on。由于您通过使用 .get()
方法获得 Variable Classes' 值(在您的情况下为 IntVar),因此您需要不同的值。因此,您需要将每个元素保存在列表中,然后对其进行迭代。
我希望它能即时创建一堆复选框,并在按下 'Submit' 按钮时查找所有选中的复选框。目前,当按 'Submit' 调用 select_adgroup()
时,它会为每个项目打印 0
,无论它们是否被选中,除非每个框都被选中,在这种情况下它会打印 1
对于每个项目。但我希望它只为选中的框打印 1
。
def search_adgroups(self):
self.adgroups = adgroup(checkBoxVal.get())
self.inc1 = 1
self.cbuts1 = []
for index, item in enumerate(self.adgroups):
self.adBoxVal = IntVar()
self.adgroup_check = (Checkbutton(self, variable=self.adBoxVal, text = item))
self.cbuts1.append(self.adgroup_check)
self.cbuts1[index].grid(row=self.inc1, sticky=W)
self.inc1 += 1
self.button2 = Button(self, text="Submit", command=self.select_adgroup)
self.button2.grid(row=self.inc1, sticky=W)
def select_adgroup(self):
for item in self.cbuts1:
print(self.adBoxVal.get())
首先它不是if all checked
,而是if last one checked
它会打印1s。所以总的来说它只打印最后一项的价值。这说明了很多问题。
在for
循环之后,self.adBoxVal
的值将是最后一个,所以当你试图在你的方法中用self.adBoxVal.get()
获取它的值时,你只会得到最后一个价值。
要解决这个问题,您需要将所有 self.adBoxVal
存储在一个列表中,然后对其进行迭代。
def search_adgroups(self):
...
...
self.chks = [] #your list for IntVars
for index, item in enumerate(self.adgroups):
adBoxVal = IntVar() #no need self here as BryanOakley points out
self.chks.append(self.adBoxVal)
#no need self at below also
adgroup_check = Checkbutton(self.root, variable=adBoxVal, text = item)
self.cbuts1.append(adgroup_check)
self.cbuts1[index].grid(row=self.inc1, sticky=W)
self.inc1 += 1
self.button2 = Button(self.root, text="Submit", command=self.select_adgroup)
self.button2.grid(row=self.inc1, sticky=W)
def select_adgroup(self):
for item in self.chks: #here you need to iterate over IntVars
#to get thier value
print (item.get())
此外,对于您以后的问题,如果您 post 一个 有效的 代码可以重现没有一些未知函数或变量的错误,那就太好了。
编辑: 让我们用 print
来说明。当您在 self.adBoxVal = IntVar()
下添加 print(self.addBoxVal)
时,您会看到每个项目都不同。
for index, item in enumerate(self.adgroups):
adBoxVal = IntVar()
print (adBoxVal)
>>>
PY_VAR0 #I assigned range(5) to adgroups that's why
PY_VAR1 #there are 5 elements here
PY_VAR2
PY_VAR3
PY_VAR4
如果您在 您的 代码中的 select_adgroup
方法中添加 print (adBoxVal)
,您将看到它是 PY_VARX
(最后一个)和代码仅适用于 with/on。由于您通过使用 .get()
方法获得 Variable Classes' 值(在您的情况下为 IntVar),因此您需要不同的值。因此,您需要将每个元素保存在列表中,然后对其进行迭代。