Kivy在.py文件中选择屏幕并在.kv文件中使用

时间:2018-03-20 13:11:16

标签: python kivy

我正在尝试制作一个屏幕列表,并在此python文件中随机选择其中一个(此处不包含其他代码,如构建器等):

class selectionScreen
    testScreen = StringProperty('Screen1')

    def screenSelector(self)
        screenList = ['Screen1', 'Screen2']
        testNumber = random.randint(0, 1)
        testScreen = screenList[testNumber]
        return testscreen

然后我想在我的.kv文件中选择这个屏幕,看起来有点像这样,但也包括屏幕1和2

ScreenManager:
    SelectionScreen:
        name: 'SelectionScreen'
    Screen0:
        name: 'Screen0'
    Screen1:
        name: 'Screen1'

<SelectionScreen>:
     BoxLayout:
        Button:
            text: "Click when ready"
            size_hint: .6, .5
            font_size: 10
            on_press: root.screenSelector()
            on_release: root.manager.current = 'Screen0'
<Screen0>:
    BoxLayout:
        orientation: 'vertical'
        Button:
            text: "Next"
            on_release: root.manager.current = str(root.testScreen)

SelectionScreen工作正常,但现在我无法访问screen0。删除行

on_press: root.screenSelector()
但是,我可以这样做。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

问题

  1. 您无法访问 screen0 ,因为它不在 screenList 中。
  2. 方法 screenSelector 中定义的变量 testScreen 是一个局部变量,它不是同一个类在 selectionScreen 类中定义的变量 testScreen
  3. 由于拼写错误,未定义的变量,返回 testscreen
  4. Screen0 ,代码 on_release:root.manager.current = str(root.testScreen)将导致 AttributeError:&# 39;位于Screen0&#39;对象没有属性&#39; testScreen&#39;
  5. 解决方案

    要访问Screen0,您必须执行以下操作:

    Python脚本

    1. Screen0 添加到变量 screenList
    2. random.randint(0,1) 替换为 random.randint(0,2) < / LI>
    3. testScreen 替换为方法 screenSelector self.testScreen >

      kv文件

      1. 定义一个ID,例如 id:selection_screen
      2. 参考,例如的 root.manager.ids.selection_screen.testScreen
      3. 在其他屏幕中添加on_press调用 screenSelector ,例如 on_press:root.manager.ids.selection_screen.screenSelector()
      4. 实施例

        main.py

        from kivy.app import App
        from kivy.uix.screenmanager import Screen, ScreenManager
        from kivy.properties import StringProperty
        import random
        
        
        class ScreenManager(ScreenManager):
            pass
        
        
        class SelectionScreen(Screen):
            testScreen = StringProperty('Screen1')
        
            def screenSelector(self):
                screenList = ['Screen0', 'Screen1', 'Screen2']
                testNumber = random.randint(0, 2)
                self.testScreen = screenList[testNumber]
                return self.testScreen
        
        
        class Screen0(Screen):
            pass
        
        
        class Screen1(Screen):
            pass
        
        
        class Screen2(Screen):
            pass
        
        
        class Test(App):
        
            def build(self):
                return ScreenManager()
        
        
        if __name__ == "__main__":
            Test().run()
        

        test.kv

        #:kivy 1.10.0
        
        <ScreenManager>:
            SelectionScreen:
                id: selection_screen
                name: 'SelectionScreen'
            Screen0:
                name: 'Screen0'
            Screen1:
                name: 'Screen1'
            Screen2:
                name: 'Screen2'
        
        <SelectionScreen>:
            BoxLayout:
                Button:
                    text: "Click when ready"
                    size_hint: .6, .5
                    font_size: 10
                    on_press: root.screenSelector()
                    on_release:
                        root.manager.current = str(root.manager.ids.selection_screen.testScreen)
        
        <Screen0>:
            BoxLayout:
                orientation: 'vertical'
                Button:
                    text: "Screen 0"
                    on_press: root.manager.ids.selection_screen.screenSelector()
                    on_release:
                        root.manager.current = str(root.manager.ids.selection_screen.testScreen)
        
        <Screen1>:
            BoxLayout:
                orientation: 'vertical'
                Button:
                    text: "Screen 1"
                    on_press: root.manager.ids.selection_screen.screenSelector()
                    on_release:
                        root.manager.current = str(root.manager.ids.selection_screen.testScreen)
        
        <Screen2>:
            BoxLayout:
                orientation: 'vertical'
                Button:
                    text: "Screen 2"
                    on_press: root.manager.ids.selection_screen.screenSelector()
                    on_release:
                        root.manager.current = str(root.manager.ids.selection_screen.testScreen)
        

        输出

        Img01 - App Startup Img02 - Screen2 Displayed Img03 - Screen0 Displayed Img04 - Screen1 Displayed Img05 - Screen2 Displayed Img06 - Screen0 Displayed