首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从不同的布局管理屏幕

如何从不同的布局管理屏幕
EN

Stack Overflow用户
提问于 2019-01-02 01:40:02
回答 1查看 24关注 0票数 0

我正在尝试使用一组按钮来控制显示哪个屏幕,同时仍然显示这些按钮。

bobo.py

代码语言:javascript
复制
import kivy
kivy.require("1.9.0")

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout

class ButtSection(BoxLayout):
    pass

class Welcome(Screen):
    pass

class AccountOne(Screen):
    pass

class AccountTwo(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

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

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

main = BoboApp()
main.run()

Bobo.kv

代码语言:javascript
复制
BoxLayout:
    orientation: "horizontal"
    BoxLayout:
        ButtSection:
            orientation: "vertical"
            Button:
                text: "Account One"
                on_press: app.root.current = "a1"
            Button:
                text: "Account Two"
                on_press: app.root.current = "a2"
            Button:
                text: "Account Three"
                on_press: app.root.current = "a3"

    ScreenManagement:
        Welcome:
            name: "wel"
            Label:
                text: "Welcome To Bobot"
        AccountOne:
            name: "a1"
            Label:
                text: "Page: Account One"
        AccountTwo:
            name: "a2"
            Label:
                text: "Page: Account One"

当我运行脚本时,欢迎屏幕是当前屏幕。当我点击一个按钮时,它什么也不做,即使我输入了'on_press: app.root.current =‘。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-02 03:13:05

您必须分析app.root的含义,看看它是否是ScreenManager。

主是指作为BoboApp实例的应用程序,即approot是指返回应用程序的build方法的对象,即presentation。presentation是.kv的根对象,也就是BoxLayout。根据我们得出的结论,app.root不是错误有效的ScreenManager。

不使用根作为访问ScreenManager的方法,可以通过id访问它,因为id在整个树中都是可访问的。

另一方面,我已经更改了屏幕的名称,以匹配您想要设置的名称。

代码语言:javascript
复制
BoxLayout:
    orientation: "horizontal"
    BoxLayout:
        ButtSection:
            orientation: "vertical"
            Button:
                text: "Account One"
                on_press: manager.current = "a1" # <---
            Button:
                text: "Account Two"
                on_press: manager.current = "a2" # <---
            Button:
                text: "Account Three"
                on_press: manager.current = "a3" # <---

    ScreenManagement:
        id: manager # <---
        Welcome:
            name: "a1" # <---
            Label:
                text: "Welcome To Bobot"
        AccountOne:
            name: "a2" # <---
            Label:
                text: "Page: Account One"
        AccountTwo:
            name: "a3" # <---
            Label:
                text: "Page: Account One"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53997568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档