Kivy屏幕管理器切换屏幕离开按钮



我正在使用屏幕管理器来滑动屏幕,但是当我正常切换屏幕时,屏幕上的按钮会保留。我必须将按钮复制到每个屏幕。有没有办法在保持按钮原位的同时切换屏幕?

这是我的应用程序代码:

import (...)

class AppContainer(FloatLayout):
pass

class NavButtons(BoxLayout):
pass

class FirstScreen(Screen):
pass

class SecondScreen(Screen):
pass

class Screens(ScreenManager):
pass

class MainApp(App):
def build(self):
return AppContainer()

(...

KV文件看起来像这样:

#:import BoxLayout kivy.uix.boxlayout.BoxLayout
<AppContainer>:
NavButtons:
Screens:
FirstScreen:
SecondScreen:
ThirdScreen:

<NavButtons>:
orientation:'vertical'
Button:
on_press: root.manager.current="first"
text: 'First'
pos_hint:{"top": 1, "left": 0}
Button:
on_press: root.manager.current="second"
text: 'Second'
pos_hint:{"top":0.8, "left": 0}
<Button>:
size_hint: 0.2, 0.2

<FirstScreen>:
name: "first"
Label:
text: "First Screen"

<SecondScreen>:
name: "second"
Label:
text: "Second Screen"

但这会引发一个错误:属性错误:"导航按钮"对象没有属性"管理器">

有什么想法吗?

在按钮的字段中,根是NavButtons,正如您所看到的NavButtons作为孩子没有屏幕,访问Screens的一种方法是通过app.root,在这种情况下返回到AppContainer,我们将其设置为Screens,我们访问它是因为他是一个孩子。

#:import BoxLayout kivy.uix.boxlayout.BoxLayout
<AppContainer>:
NavButtons:
Screens:
id: sm
FirstScreen:
SecondScreen:
<NavButtons>:
orientation:'vertical'
Button:
on_press: app.root.ids.sm.current="first"
text: 'First'
pos_hint:{"top": 1, "left": 0}
Button:
on_press: app.root.ids.sm.current ="second"
text: 'Second'
pos_hint:{"top":0.8, "left": 0}
[...]

最新更新