我正在尝试使用一组按钮来控制显示哪个屏幕,同时仍然显示这些按钮。
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 =‘。
发布于 2019-01-02 03:13:05
您必须分析app.root的含义,看看它是否是ScreenManager。
主是指作为BoboApp实例的应用程序,即app。root是指返回应用程序的build方法的对象,即presentation。presentation是.kv的根对象,也就是BoxLayout。根据我们得出的结论,app.root不是错误有效的ScreenManager。
不使用根作为访问ScreenManager的方法,可以通过id访问它,因为id在整个树中都是可访问的。
另一方面,我已经更改了屏幕的名称,以匹配您想要设置的名称。
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"https://stackoverflow.com/questions/53997568
复制相似问题