如何从其他布局管理屏幕

时间:2019-01-01 17:40:02

标签: python-3.x kivy

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

bobo.py

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

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 =''。

1 个答案:

答案 0 :(得分:0)

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

app指的是BoboApp实例即主程序的应用程序。 root是指返回应用程序的构建方法(即表示形式)的对象。表示是.kv的根对象,即BoxLayout。得出的结论是,app.root不是错误有效的ScreenManager。

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

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

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"