Kivy:如何检索python中创建的复选框(或其他小部件)的ID或活动状态

Kivy:如何检索python中创建的复选框(或其他小部件)的ID或活动状态,python,checkbox,kivy,Python,Checkbox,Kivy,我的应用程序遵循3个步骤: 在步骤1中,用户输入一个数字(所有小部件都在.kv文件中-参见下面的代码) 在步骤2中,生成与步骤1中输入的数字相同数量的标签和复选框。然后用户选择一些复选框并单击按钮“OK 2”。(因为第二步的小部件数量可能不同,它们是在.py中创建的。这可能不是最好的方法,但我还没有找到更好的方法) 在第3步中,我得到了在第2步中生成的复选框的活动状态,根据哪个复选框处于活动状态,我执行了更多的步骤 我的问题是如何获取复选框的状态?当它们被“创建”时,每个都有一个id,但当我

我的应用程序遵循3个步骤:

  • 在步骤1中,用户输入一个数字(所有小部件都在
    .kv
    文件中-参见下面的代码)
  • 在步骤2中,生成与步骤1中输入的数字相同数量的标签和复选框。然后用户选择一些复选框并单击按钮“OK 2”。(因为第二步的小部件数量可能不同,它们是在
    .py
    中创建的。这可能不是最好的方法,但我还没有找到更好的方法)
  • 在第3步中,我得到了在第2步中生成的复选框的活动状态,根据哪个复选框处于活动状态,我执行了更多的步骤
我的问题是如何获取复选框的状态?当它们被“创建”时,每个都有一个id,但当我打印
self.id
时,这些id不会出现。如果我将任何参数传递给
getcheckbox\u active
def,我也会得到一个错误。(无不可调用)

.py

.kv
:我需要一个.kv,因为“step 1 real app”比文本输入和按钮复杂得多


方向:“水平”
文本输入:
text:root.input\u text
id:输入
按钮:
文本:“确定1”
按:root.show(input.text)

这里的问题是
ids
字典中只填充了.kv文件中定义的
id
值,而不是python中定义的

但是,您可以创建自己的字典,其中包含对
复选框的引用。您可以填充
MyWidget
(我们称之为
check\u ref
)的字典属性,而不是在创建小部件时提供
id
属性,该属性将
id
与每个
复选框
实例链接:

class MyWidget(BoxLayout):
    input_text = StringProperty("10")

    check_ref = {}

    def show(self, number):
        layout = BoxLayout(padding=10, orientation="vertical")
        for each in range(int(number)):
            layout2 = BoxLayout(padding=10, orientation="horizontal")
            l=Label(bold= True,font_size=20, text='Hello', markup = True)
            c = CheckBox()

            # Stores a reference to the CheckBox instance 
            self.check_ref["CheckBox"+str(each)] = c

            layout2.add_widget(l)
            layout2.add_widget(c)
            layout.add_widget(layout2)
        button = Button(text="OK 2")
        button.bind(on_press=self.getcheckboxes_active)  # self.getcheckboxes_active(self, "test") give an error None is not callable
        layout.add_widget(button)
        self.add_widget(layout)

        self.input_text = "Done"

    def getcheckboxes_active(self, *arg):
        '''how to get the active state of all checkboxed created in def show'''
        # Iterate over the dictionary storing the CheckBox widgets
        for idx, wgt in self.check_ref.items():
             print(wgt.active)

        # You can also get a specific CheckBox
        # print(self.check_ref[--my id--].active)

可能是一种常见的情况:从字符串列表中,使用前面提到的字典概念制作标签及其对应的复选框,然后将选中的复选框标签显示为另一个标签的文本

class BuildRequester(BoxLayout):
    chkref = {}
    def on_checkbox_active(self,chkbox,value):
        self.ids.label2.text = 'Selected ' + self.chkref[chkbox]
    def __init__(self, **kwargs):
        super(BuildRequester,self).__init__(**kwargs)
        prods = [' B_0003',' B_0007',' B_0008', ' B_0200']

        for i in range(4):
            self.add_widget(Label(text=prods[i],italic=True,bold=True))
            chkbox = CheckBox(group='1',color=[0.1,1,0,4])
            chkbox.bind(active=self.on_checkbox_active)
            self.add_widget( chkbox)
            self.chkref[chkbox]= prods[i]

谢谢!我只是对最后一个注释的打印有问题:
self.check\u-ref[CheckBox0]。状态
,我得到错误
名称“CheckBox0”没有定义
,即使
print(self.check\u-ref)
没有打印
CheckBox0
id。
CheckBox0
应该是字符串吗?很抱歉!最后一个问题。为什么属性是
state
,有两个值
normal
down
,而对于
.kv
中的复选框,属性是
active
,值是
True
False
?哎呀,我的坏!没错,它应该是
活动的
属性,而不是
状态
。我会更正它。您可以,但您必须使用
部分
或匿名函数。在您的示例中,您可以执行如下操作:
button.bind(on\u press=lambda x:self.getcheckbox\u active(“teste”))
。这也可能有助于:
class BuildRequester(BoxLayout):
    chkref = {}
    def on_checkbox_active(self,chkbox,value):
        self.ids.label2.text = 'Selected ' + self.chkref[chkbox]
    def __init__(self, **kwargs):
        super(BuildRequester,self).__init__(**kwargs)
        prods = [' B_0003',' B_0007',' B_0008', ' B_0200']

        for i in range(4):
            self.add_widget(Label(text=prods[i],italic=True,bold=True))
            chkbox = CheckBox(group='1',color=[0.1,1,0,4])
            chkbox.bind(active=self.on_checkbox_active)
            self.add_widget( chkbox)
            self.chkref[chkbox]= prods[i]