Python Kivy屏幕管理器Wietet范围 [英] Python Kivy screen manager wiget scope

查看:92
本文介绍了Python Kivy屏幕管理器Wietet范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过一个单独的类中的按钮来控制屏幕管理器,但是我无法弄清楚在按钮on_press:语句上要设置什么.

I am trying to control a screen manager from buttons in a separate class, but I cannot figure out what to set on the button on_press: statements.

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'

我觉得这是一个范围界定问题,错误提示如下:

I feel like it is a scoping issue, errors say things like:

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

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

So my app has the following hierarchy:

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

因此,我需要遍历FloatLayout的一个级别,然后向下钻取ContentSection来访问屏幕管理器.

So I need to traverse up a level into FloatLayout, then drill down into ContentSection to access the screen manager.

推荐答案

导航小部件树对我来说很痛苦,而且AFAIK不能以您想要的方式遍历小部件树.

Navigating widget trees has been a pain for me, and AFAIK you can't traverse the widget tree the way you'd like.

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

You can, however, simplify your widget tree, make sure everything shares the same root, and use ids.

这就是我的做法(我也将所有内容移至了kv语言):

Here's how I did it (I also moved everything to kv language):

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'

python

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()

这篇关于Python Kivy屏幕管理器Wietet范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆