我正在为学校建立一个小项目,我需要一个界面,所以我想我应该使用Kivy
和Python
,但我被UI
卡住了,主要是如何跨树访问widgets。我到处搜索,但kivy
文档帮助不大,我发现的问题和答案似乎没有关系。
我想做的是在顶部有一个带有一些按钮的栏,在屏幕管理器下面有一个单独的预定义屏幕,但我不确定如何才能遍历树来完成我需要的事情。
例如,第一个按钮总是会弹出一个名为Dashboard的屏幕。我尝试了一些我找到的东西,但是我得到了"Name Dashboard something“
#!/usr/bin/env python
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.properties import ObjectProperty
class Container(FloatLayout):
pass
class UpperMenu(BoxLayout):
def change_screen(self,argument):
self.ids.screens.ids.manager.current = argument
class Screens(ScreenManager):
pass
class Dashboard(Screen):
pass
class Player(Screen):
pass
class Weather(Screen):
pass
class Map(Screen):
pass
class Carousel(BoxLayout):
pass
class CarlApp(App):
def build(self):
return Container()
if __name__ == '__main__':
CarlApp().run()
这是我的KV文件:
#:kivy 1.10.0
<Container>:
id: container
UpperMenu:
id: uppermenu
pos: root.x, root.top - self.height
Screens:
id: screens
<UpperMenu>:
size_hint: 1,.15
Button:
text: ""
id: ButtonCeasuri
on_release: root.change_screen(dashboard)
Image:
source: 'dashboard.png'
allow_stretch: False
center_x: self.parent.center_x
center_y: self.parent.center_y
Button:
text: ""
id: ButtonCeasurii
Image:
source: 'play-button.png'
allow_stretch: False
center_x: self.parent.center_x
center_y: self.parent.center_y
Button:
text: ""
id: ButtonCeasuriii
Image:
source: 'distance.png'
allow_stretch: False
center_x: self.parent.center_x
center_y: self.parent.center_y
Button:
text: ""
id: ButtonCeasuriiii
Image:
source: 'temperature.png'
allow_stretch: False
center_x: self.parent.center_x
center_y: self.parent.center_y
<Screens>:
size_hint: 1,.85
Dashboard:
name: 'Dashboard'
label: "Dashboard Screen"
id: dashboard
Button:
text: "Stuff"
on_release: pass
Player:
name: 'Player'
label: "Player Screen"
id: Player
Button:
text: "Stuff2"
Weather:
name: 'Weather'
label: "Weather Screen"
Map:
name: 'Map'
label: "Map Screen"
在过去的3-4天里,我一直在试图理解我是如何做到这一点的,但我无法理解。
发布于 2018-06-18 06:06:47
从你的代码看,你似乎已经在仪表板屏幕上了,不管怎么说,要将你的屏幕切换到另一个屏幕,这是一个示例代码,可以帮助你做到这一点
<UpperMenu>:
size_hint: 1,.15
Button:
text: ""
id: ButtonCeasuri
on_release: root.change_screen("Dashboard")
名称仪表板是未定义的,因为kv文件不能引用它,然后在您的代码中也可以这样做,
class UpperMenu(BoxLayout):
def change_screen(self,argument):
self.parent.children[0].current = argument
您可以调用print(self.parent.children)
查看它输出的内容,我希望这对您有所帮助
https://stackoverflow.com/questions/50898857
复制相似问题