[英]Python Kivy Traversing the Widget tree
我正在為學校建設一個小型項目,並且需要一個接口,所以我以為我會使用Kivy
和Python
,但是我陷入UI
的一部分,主要是如何在樹上訪問小部件。 我進行了搜索,但是kivy
文檔不是很有幫助,發現的問題和答案似乎無關。
我想要做的是在頂部的按鈕欄和一個單獨的預定義屏幕的屏幕管理器下方,但是我不確定如何遍歷樹以完成所需的操作。
因此,例如,第一個按鈕將始終顯示稱為儀表板的屏幕。 我已經嘗試了一些發現的問題,但是卻收到“未定義名稱儀表盤”的信息
#!/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天該怎么做,但是我不明白。
從您的代碼看來,您似乎已經在儀表板屏幕上了,無論如何將您的屏幕更改為另一個屏幕,這是一個示例代碼,可以幫助您實現
<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)
查看打印出來的內容,希望對您print(self.parent.children)
幫助
要更改屏幕,有兩種方法可以完成。 第一種方法都是在kv文件中完成的。 第二種方法在Python文件中完成。 在示例中演示了這兩種方法。
您可以通過以下方式將用於更改屏幕的代碼放在kv文件中:
root.change_screen(dashboard)
與
app.root.ids.screen.current = 'Dashboard' # 'Dashboard' name of screen
root.change_screen('Dashboard')
。 self.ids.screens.ids.manager.current
與self.parent.ids.screen.current
在change_screen()
方法。 self.ids.screens.ids.manager.current = argument
AttributeError: 'super' object has no attribute '__getattr__'
self-關鍵字self引用“當前小部件實例”,即類UpperMenu()
ids-關鍵字id引用查找對象,它是字典類型的屬性。 在這種情況下,所有kv文件中在類規則<UpperMenu>:下標記有id的Button小部件。
屏幕 -關鍵字屏幕參考ID對 屏幕 ,但它並沒有下self.ids存在。 因此,Kivy拋出了AttributeError。 添加print(self.ids)以顯示在self.ids中找到的所有ID 。
#: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') # Method 2
Image:
source: 'dashboard.png'
allow_stretch: False
center_x: self.parent.center_x
center_y: self.parent.center_y
Button:
text: ""
id: ButtonCeasurii
on_release:
app.root.ids.screens.current = 'Player' # Method 1
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:
id: dashboard
name: 'Dashboard'
label: "Dashboard Screen"
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"
#!/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.parent.ids.screens.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 TestApp(App):
def build(self):
return Container()
if __name__ == '__main__':
TestApp().run()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.