Kivy / Python异常:无法在隐藏的小部件上打开下拉列表

时间:2016-02-06 18:16:45

标签: python kivy dropdown

我正在为具有许多不同屏幕的触摸屏构建UI。一个屏幕需要有一个下拉列表来选择一种语言。我已经尝试了许多创建此下拉列表的方法但没有成功。最近,我收到了这个错误:

Traceback (most recent call last):
File "/Users/revascharf/Documents/COLLEGE WORK/SENIOR YEAR/screensTest/screen.py", line 91, in <module>
presentation = Builder.load_file("screen.kv")
File "/Applications/Kivy.app/Contents/Resources/kivy/kivy/lang.py", line 1842, in load_file
 return self.load_string(data, **kwargs)
File "/Applications/Kivy.app/Contents/Resources/kivy/kivy/lang.py", line 1921, in load_string
 self._apply_rule(widget, parser.root, parser.root)
File "/Applications/Kivy.app/Contents/Resources/kivy/kivy/lang.py", line 2082, in _apply_rule
 child = cls(__no_builder=True)
File "/Users/revascharf/Documents/COLLEGE WORK/SENIOR YEAR/screensTest/screen.py", line 85, in __init__
 ddbutt.bind(on_release=dropdown.open(self))
File "/Applications/Kivy.app/Contents/Resources/kivy/kivy/uix/dropdown.py", line 220, in open
 'Cannot open a dropdown list on a hidden widget')
kivy.uix.dropdown.DropDownException: Cannot open a dropdown list on a hidden widget

从我在网上看到的内容来看,这可能是屏幕管理器和下拉不兼容的问题。有人能指出我哪里出错了吗?问题在于CreateProfileScreen。

screen.py:

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


class HomeScreen(Screen):
    pass

class SettingsScreen(Screen):
    pass

class StartUserRunScreen(Screen):
    pass

class ManageUserProfilesScreen(Screen):
    pass

class InitialPanelConfigScreen(Screen):
    panel_connect = StringProperty()
    panelNo = 0

    def __init__(self, **kwargs):
        super(InitialPanelConfigScreen, self).__init__(**kwargs)
        self.panel_connect = 'Connect Panel: ' + str(self.panelNo)

    def panelConnected(self):
        #python I2C code goes here to send actual address
        if self.panelNo < 20:
            self.panelNo += 1
            self.panel_connect = 'Connect Panel: ' + str(self.panelNo)

    def cancelButton(self):
        #app.root.current = 'settings'
        self.panelNo = 0
        self.panel_connect = 'Connect Panel: ' + str(self.panelNo)

class AdjustVolumeScreen(Screen):
    pass

class CreateProfileScreen(Screen):
    def __init__(self, *args, **kwargs):
        super(CreateProfileScreen, self).__init__(*args, **kwargs)
        dropdown = DropDown()
        languages = ['English', 'Spanish', 'French', 'Cantonese']

        for lang in languages:
            btn = Button(text='%r' % lang, size_hint_y=.26, size_hint_x=.085)
            btn.bind(on_release=lambda btn: dropdown.select(btn.text))
            dropdown.add_widget(btn)
        ddbutt = ObjectProperty(None)
        ddbutt.bind(on_release=dropdown.open(self))
        dropdown.bind(on_select=lambda instance, x: setattr(ddbutt, 'text', x))

class ScreenManagement(ScreenManager):
    pass

presentation = Builder.load_file("screen.kv")

class MainApp(App):
    def build(self):
        return presentation

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

screen.kv:

#: import sm kivy.uix.screenmanager
ScreenManagement:
    transition: sm.NoTransition()
    HomeScreen:
    SettingsScreen:
    StartUserRunScreen:
    ManageUserProfilesScreen:
    InitialPanelConfigScreen:
    AdjustVolumeScreen:
    CreateProfileScreen:

<HomeScreen>:
    name: 'home'
    FloatLayout:
        Label:
            text: 'Carter School Sensory Walk'
            font_size: 40
            pos_hint: {'x': 0, 'y': .4}

        Image:
            source: 'SensoryWalkLogo.png'
            size_hint: .68, .68
            pos_hint: {'x': -.045, 'y': .09}

        Button:
            on_press: app.root.current = 'settings'
            text: 'Settings'
            font_size: 20
            size_hint: .27, .12
            pos_hint: {'x': .63, 'y': .33}

        Button:
            on_press: app.root.current = 'startrun'
            text: 'Start User Run'
            font_size: 20
            size_hint: .27, .12
            pos_hint: {'x': .63, 'y': .63}

        Button:
            on_press: app.root.current = 'manageprofiles'
            text: 'Manage User Profiles'
            font_size: 20
            size_hint: .27, .12
            pos_hint: {'x': .63, 'y': .48}

        Button:
            text: 'Exit'
            font_size: 20
            size_hint: .27, .12
            pos_hint: {'x': .63, 'y': .18}
            on_press: exit()



<SettingsScreen>:
    name: 'settings'

    FloatLayout:
        Label:
            text: 'Settings'
            font_size: 40
            pos_hint: {'x': 0, 'y': .4}

        Button:
            on_press: app.root.current = 'home'
            text: 'Return Home'
            font_size: 17
            size_hint: .189, .09
            pos_hint: {'x': .811, 'y': 0}

        Button:
            text: 'Adjust Brightness'
            font_size: 20
            size_hint: .27, .12
            pos_hint: {'x': .1, 'y': .63}

        Button:
            text: 'Adjust Volume'
            font_size: 20
            size_hint: .27, .12
            pos_hint: {'x': .1, 'y': .48}
            on_press: app.root.current = 'volume'

        Button:
            text: 'Panel Replacement'
            font_size: 20
            size_hint: .27, .12
            pos_hint: {'x': .63, 'y': .63}

        Button:
            text: 'Initial Panel Configuration'
            font_size: 17.5
            size_hint: .27, .12
            pos_hint: {'x': .63, 'y': .48}
            on_press: app.root.current = 'initpanelconfig'

<StartUserRunScreen>:
    name: 'startrun'

    FloatLayout:
        Label:
            text: 'Select User'
            font_size: 40
            pos_hint: {'x': 0, 'y': .4}

        Button:
            on_press: app.root.current = 'home'
            text: 'Return Home'
            font_size: 17
            size_hint: .189, .09
            pos_hint: {'x': .811, 'y': 0}


<ManageUserProfilesScreen>:
    name: 'manageprofiles'

    FloatLayout:
        Label:
            text: 'Manage User Profiles'
            font_size: 40
            pos_hint: {'x': 0, 'y': .4}

        Button:
            on_press: app.root.current = 'home'
            text: 'Return Home'
            font_size: 17
            size_hint: .189, .09
            pos_hint: {'x': .811, 'y': 0}

        Button:
            text: 'Create New User Profile'
            font_size: 30
            size_hint: .5, .15
            pos_hint: {'x': .25, 'y': .6}
            on_press: app.root.current = 'createprofile'

        Button:
            text: 'Edit Existing User Profile'
            font_size: 30
            size_hint: .5, .15
            pos_hint: {'x': .25, 'y': .4}

        Button:
            text: 'Delete User Profile'
            font_size: 30
            size_hint: .5, .15
            pos_hint: {'x': .25, 'y': .2}

<InitialPanelConfigScreen>:
    name: 'initpanelconfig'

    FloatLayout:
        Label:
            text: 'Initial Panel Configuration'
            font_size: 40
            pos_hint: {'x': 0, 'y': .4}

        Button:
            text: 'Cancel'
            font_size: 20
            size_hint: .189, .09
            pos_hint: {'x': .811, 'y': 0}
            on_press:
                app.root.current = 'settings'
                root.cancelButton()

        Button:
            text: 'Panel Connected'
            font_size: 28
            size_hint: .3, .12
            pos_hint: {'x': .35, 'y': .1}
            on_press: root.panelConnected()

        Label:
            text: root.panel_connect
            font_size: 32
            pos_hint: {'x': 0, 'y': .04}

<AdjustVolumeScreen>:
    name: 'volume'

    FloatLayout:
        Label:
            text: 'Settings: Volume'
            font_size: 40
            pos_hint: {'x': 0, 'y': .4}

        Button:
            text: 'Return to Settings'
            font_size: 17
            size_hint: .189, .09
            pos_hint: {'x': .811, 'y': 0}
            on_press: app.root.current = 'settings'

<CreateProfileScreen>:
    name: 'createprofile'
    ddbutt: langbutt

    FloatLayout:
        Label:
            text: 'New Profile'
            font_size: 40
            pos_hint: {'x': 0, 'y': .4}

        Button:
            text: 'Create Profile'
            font_size: 30
            size_hint: .26, .12
            pos_hint: {'x': .37, 'y': .03}

        Button:
            text: 'Cancel'
            font_size: 20
            size_hint: .189, .09
            pos_hint: {'x': .811, 'y': 0}
            on_press: app.root.current = 'manageprofiles'

        TextInput:
            size_hint: .26, .075
            pos_hint: {'x': .23, 'y': .71}
            multiline: False

        Label:
            text: 'User Name:'
            font_size: 25
            pos_hint: {'x': -.38, 'y': .25}

        Label:
            text: ' Language \nPreference:'
            font_size: 23
            pos_hint: {'x': -.38, 'y': 0}

        Button:
            id: langbutt
            size_hint: .26, .085
            pos_hint: {'x': .23, 'y': .46}
            font_size: 20
            text: 'Choose a Language'

1 个答案:

答案 0 :(得分:0)

没有弄清楚如何解决下拉问题,但发现对于我需要的东西,一个微调器也可以正常工作。我能够实现一个微调器代替下拉列表。