在选择猕猴桃中的下拉值时,如何在屏幕之间导航?

时间:2019-12-05 09:14:35

标签: python kivy kivy-language

我的kivy应用程序有多个屏幕: 1.主屏幕,屏幕1,屏幕2,屏幕3和屏幕4。

主屏幕具有主下拉列表和子下拉列表。主下拉菜单显示组,选择特定组后,子下拉菜单显示与该组关联的屏幕。

从子下拉列表中选择后,我想导航到特定屏幕。我的代码没有显示任何错误,但是我无法导航到所选屏幕。请帮忙。

doca.py

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ListProperty, DictProperty

class windowManager(ScreenManager):
    pass

class MainScreen(Screen):
    values_dict = {'Group1':['Screen1', 'Screen2'],
              'Group2':['Screen3', 'Screen4']}

    sub_values = ListProperty()

    def values_update(self,text):
        self.sub_values = self.values_dict[text]
        if text != 'Select group type':
            self.ids.sub_drop.text = 'select ' + text + ' screen'
            return 'select ' + text + ' screen'

    def open_screen(self, text):        
        if text != 'select ' + self.ids.main_drop.text + ' screen':
            sm = windowManager()
            sm.current = text

class Screen1(Screen):    
    pass

class Screen2(Screen):
    pass

class Screen3(Screen):
    pass

class Screen4(Screen):
    pass

class appln(App):                
    def build(self):    
        return windowManager()            

if __name__=="__main__":
    appln().run()

appln.kv

<WindowManager>:
    MainScreen:
    Screen1:
    Screen2:
    Screen3:
    Screen4:


<MainScreen>:
    name: 'main'

    GridLayout:
        cols:2
        Spinner:
            id: main_drop
            size_hint: None, None
            size: 200, 100
            pos_hint:{'top': 1}
            text: 'Select group type'
            values: root.values_dict.keys()
            on_text:
                root.values_update(self.text)

        Spinner:
            id: sub_drop
            size_hint: None, None
            size: 230, 100
            pos_hint:{'top': 1}
            values: root.sub_values
            on_text: root.open_screen(self.text)


<Screen1>:
    name: 'Screen1'
    Label:
        text: 'This is screen 1'

<Screen2>:
    name: 'Screen2'
    Label:
        text: 'This is screen 2'

<Screen3>:
    name: 'Screen3'
    Label:
        text: 'This is screen 3'

<Screen4>:
    name: 'Screen4'
    Label:
        text: 'This is screen 4'

1 个答案:

答案 0 :(得分:0)

在您的MainScreen代码中,该行:

sm = windowManager()

正在创建windowManager的新实例,因此对该实例的任何使用将对实际上管理您的windowManager的{​​{1}}无效。要解决此问题,只需使用对正确的Screens的引用,只需将该行替换为:

windowManager