Python Kivy屏幕管理器wiget范围

时间:2017-03-05 21:01:39

标签: python scope kivy

我正在尝试从单独的类中的按钮控制屏幕管理器,但我无法弄清楚按钮on_press:语句上的设置。

Kivy Python Nav

Kivy档案:

<HeaderSection>:
    anchor_x: 'center'
    anchor_y: 'top'
    BoxLayout:
        orientation: 'horizontal'
        size_hint: 1, .1
        id: header
        Label:
            text: 'My App'

<ContentSection>:
    anchor_x: 'center'
    anchor_y: 'center'
    ScreenManager:
        size_hint: 1, .8
        Screen:
            name: 'home'
            Label:
                text: 'First screen'
        Screen:
            name: 'second'
            Label:
                text: 'Second screen'
        Screen:
            name: 'third'
            Label:
                text: 'Third screen'

<FooterSection>:
    anchor_x: 'center'
    anchor_y: 'bottom'
    BoxLayout:
        orientation: 'horizontal'
        size_hint: 1, .1
        Button:
            text: 'first'
            on_press: root.ContentSection.manager.current = 'first'
        Button:
            text: 'second'
            on_press: root.current = 'second'
        Button:
            text: 'third'
            on_press: ContentSection.ScreenManager.current = 'third'

Python文件:

from kivy.app import App
from kivy.lang import Builder
Builder.load_file('MyApp.kv')
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.uix.image import Image

# Declare sections
class HeaderSection(AnchorLayout):
    pass

class ContentSection(AnchorLayout):
    def build(self):

        # Create the screen manager
        sm = ScreenManager()
        sm.add_widget(FirstScreen(name='first'))
        sm.add_widget(SecondScreen(name='second'))
        sm.add_widget(ThirdScreen(name='third'))
        return sm

class FooterSection(AnchorLayout):
    pass


class MyAppApp(App):
    def build(self):

        #Create the sections

        fl = FloatLayout()
        hs = HeaderSection()
        cs = ContentSection()
        fs = FooterSection()

        fl.add_widget(hs)
        fl.add_widget(cs)
        fl.add_widget(fs)
        return fl


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

我尝试了各种方法:

on_press: root.parent.ContentSection.ScreenManager.current = 'home'
on_press: root.parent.ContentSection.manager.current = 'home'
on_press: root.ContentSection.manager.current = 'home'

我觉得这是一个范围问题,错误说的是:

AttributeError: 'FooterSection' object has no attribute 'ContentSection'

所以我的应用程序具有以下层次结构:

FloatLayout
    HeaderSection
    ContentSection
        ScreenManager
             FirstScreen
             SecondScreen
             ThirdScreen
    FooterSection
        Button for FirstScreen
        Button for SecondScreen
        Button for ThirdScreen

所以我需要将一个级别遍历到FloatLayout,然后深入到ContentSection以访问屏幕管理器。

1 个答案:

答案 0 :(得分:1)

导航窗口小部件树对我来说很痛苦,而AFAIK则无法按照您喜欢的方式遍历窗口小部件树。

但是,您可以简化窗口小部件树,确保所有内容共享相同的根,并使用ID。

我是这样做的(我也将所有内容都移到了kv语言中):

KV

FloatLayout:
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'top'
        Label:
            size_hint: 1, .1
            text: 'My App'
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'center'
        ScreenManager:
            id: manager
            size_hint: 1, .8
            Screen:
                name: 'first'
                Label:
                    text: 'First screen'
            Screen:
                name: 'second'
                Label:
                    text: 'Second screen'
            Screen:
                name: 'third'
                Label:
                    text: 'Third screen'
    AnchorLayout:
        anchor_x: 'center'
        anchor_y: 'bottom'
        BoxLayout:
            orientation: 'horizontal'
            size_hint: 1, .1
            Button:
                text: 'first'
                on_press: root.ids.manager.current = 'first'
            Button:
                text: 'second'
                on_press: root.ids.manager.current = 'second'
            Button:
                text: 'third'
                on_press: root.ids.manager.current = 'third'

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.uix.image import Image



class MyAppApp(App):
    def build(self):
        return Builder.load_file('MyApp.kv')


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