ScreenManager与Kivy中的DropDown兼容吗?

时间:2015-11-26 04:03:57

标签: python kivy

我想在Kivys ScreenManager管理的第二个屏幕中生成一个下拉列表。如果我这样做,我会得到这个追溯:

    ... 
File "C:/Users/ORANG/PycharmProjects/waldi/playground/cw.py", line 76, in on_text
         instance.drop_down.open(instance)
File "C:\Kivy-1.9.0-py2.7-win32-x64\kivy27\kivy\uix\dropdown.py", line 215, in open
         'Cannot open a dropdown list on a hidden widget')
     kivy.uix.dropdown.DropDownException: Cannot open a dropdown list on a hidden widget

这是代码,与此基本相同 example,只是简单地嵌入到屏幕管理器场景中:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.textinput import TextInput
from kivy.properties import ListProperty, StringProperty
import re
from kivy.lang import Builder

Builder.load_string('''
<Lieferant>:
    ComboLayout:
        Label:
            text: 'Label'
        ComboEdit:
            size_hint: .5, .3
            pos_hint: {'center':(.5, .5)}
            # `args` is the keyword for arguments passed to `on_text` in kv language
            on_text: self.parent.on_text(self, args[1])
''')

class ComboEdit(TextInput):
    """
    This class defines a Editable Combo-Box in the traditional sense
    that shows it's options
    """
    options = ListProperty(('',))
    '''
    :data:`options` defines the list of options that will be displayed when
    touch is released from this widget.
    '''

    def __init__(self, **kw):
        ddn = self.drop_down = DropDown()
        ddn.bind(on_select=self.on_select)
        super(ComboEdit, self).__init__(**kw)

    def on_options(self, instance, value):
        ddn = self.drop_down
        # clear old options
        ddn.clear_widgets()
        for option in value:
            # create a button for each option
            but = Button(text=option,
                         size_hint_y=None,
                         height='36sp',
                         # and make sure the press of the button calls select
                         # will results in calling `self.on_select`
                         on_release=lambda btn: ddn.select(btn.text))
            ddn.add_widget(but)

    def on_select(self, instance, value):
        # on selection of Drop down Item... do what you want here
        # update text of selection to the edit box
        self.text = value

class ComboLayout(BoxLayout):
    rtsstr = StringProperty("".join(("Substrate1,,,Substrate1,,,Substrate1,,,",
                                     "Substrate1,,,Substrate1,,,Substrate_coating",
                                     ",,,silicon,,,silicon_Substrate,,,substrate_",
                                     "silicon,,,")))
    def on_text(self, instance, value):
        if value == '':
            instance.options = []
        else:
            match = re.findall("(?<=,{3})(?:(?!,{3}).)*?%s.*?(?=,{3})" % value, \
                               self.rtsstr, re.IGNORECASE)
            # using a set to remove duplicates, if any.
            instance.options = list(set(match))
        instance.drop_down.open(instance)
class Intro(Screen):
    pass
class Lieferant(Screen):
    pass

class CWApp(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(Intro(name='Intro'))
        sm.add_widget(Lieferant(name='Lieferant'))
        return sm

if __name__ == '__main__':
    CWApp().run()

是否可以将它们组合起来?你会怎么做? 如果我只是注释掉这一行,这个代码就会运行,这会在屏幕前面添加一个下拉屏幕:

sm.add_widget(Intro(name='Intro'))

1 个答案:

答案 0 :(得分:0)

好的 - 对于这个问题,我得到了&#34; Tumbleweed Badget&#34; - 大声笑 我承认这是一个非常特别的,很明显,我在这里是多少初学者。所以我根据同样的问题提出了另一个问题,并花了一些精力让它更容易理解代码并重现它。这得到了回报!如果你曾经遇到过这样的问题,请在这里寻找解决方案:Is it a bug in Kivy? DropDown + ScreenManager not working as expected