首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Kivy:如何检索python中创建的复选框(或其他小部件)的ids或活动状态

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

Stack Overflow用户
提问于 2017-03-03 13:12:44
回答 2查看 2.4K关注 0票数 1

我的应用程序有三个步骤:

  • 在步骤1中,用户输入一个数字(所有小部件都在下面的代码所在的.kv文件-cf中)。
  • 在步骤2中,生成与步骤1中输入的数字一样多的标签和复选框。然后,用户选择一些复选框并单击按钮"OK 2“。(因为第二步的小部件数量可能有所不同,所以在.py -it中创建它们可能不是最好的方法,但我没有找到更好的方法)。
  • 在步骤3中,我得到了在步骤2中生成的复选框的活动状态,并且根据其中一个是活动的还是不活动的,我做了更多的步骤。

我的问题是如何获得复选框的状态?当它们被“创建”时,每个id都有一个id,但是当我打印self.ids时,这些id不会出现。如果我将任何参数传递给getcheckboxes_active def,也会得到一个错误。(没有一个是不可调用的)。

.py

代码语言:javascript
代码运行次数:0
运行
复制
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.checkbox import CheckBox 
from kivy.uix.button import Button 
from kivy.properties import StringProperty

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


    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(id = "CheckBox"+str(each))
            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'''
        print(self.ids)  # CheckBoxes id aren't displayed
        print(*arg)
        print("State of all checkboxes")

class MyApp_auto(App):
    def build(self):
        return MyWidget()
MyApp_auto().run()

.kv:我需要一个.kv,因为“第一步真正的应用程序”比TextInput和Button要复杂得多。

代码语言:javascript
代码运行次数:0
运行
复制
<MyWidget>
    orientation: "horizontal"
    TextInput:
        text: root.input_text
        id:input
    Button:
        text:'OK 1'
        on_press: root.show(input.text)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-03-03 14:50:18

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

但是,您可以创建自己的字典,其中包含对CheckBox小部件的引用。与其在创建小部件时提供id属性,不如填充一个MyWidget的字典属性(让我们称之为check_ref),它将您的id与每个CheckBox实例链接起来:

代码语言:javascript
代码运行次数:0
运行
复制
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)
票数 3
EN

Stack Overflow用户

发布于 2018-02-21 23:37:38

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

代码语言:javascript
代码运行次数:0
运行
复制
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]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42579840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档