0
我想從一個單獨的類中的按鈕控制屏幕管理器,但我無法弄清楚按鈕on_press:
聲明上設置什麼。Python的基維屏幕管理器的wiget範圍
Kivy文件:
<HeaderSection>:
anchor_x: 'center'
anchor_y: 'top'
BoxLayout:
orientation: 'horizontal'
size_hint: 1, .1
id: header
Label:
text: 'My App'
<ContentSection>:
anchor_x: 'center'
anchor_y: 'center'
ScreenManager:
size_hint: 1, .8
Screen:
name: 'home'
Label:
text: 'First screen'
Screen:
name: 'second'
Label:
text: 'Second screen'
Screen:
name: 'third'
Label:
text: 'Third screen'
<FooterSection>:
anchor_x: 'center'
anchor_y: 'bottom'
BoxLayout:
orientation: 'horizontal'
size_hint: 1, .1
Button:
text: 'first'
on_press: root.ContentSection.manager.current = 'first'
Button:
text: 'second'
on_press: root.current = 'second'
Button:
text: 'third'
on_press: ContentSection.ScreenManager.current = 'third'
的Python文件:
from kivy.app import App
from kivy.lang import Builder
Builder.load_file('MyApp.kv')
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.uix.image import Image
# Declare sections
class HeaderSection(AnchorLayout):
pass
class ContentSection(AnchorLayout):
def build(self):
# Create the screen manager
sm = ScreenManager()
sm.add_widget(FirstScreen(name='first'))
sm.add_widget(SecondScreen(name='second'))
sm.add_widget(ThirdScreen(name='third'))
return sm
class FooterSection(AnchorLayout):
pass
class MyAppApp(App):
def build(self):
#Create the sections
fl = FloatLayout()
hs = HeaderSection()
cs = ContentSection()
fs = FooterSection()
fl.add_widget(hs)
fl.add_widget(cs)
fl.add_widget(fs)
return fl
if __name__ == '__main__':
MyAppApp().run()
我已經嘗試了各種方法:
on_press: root.parent.ContentSection.ScreenManager.current = 'home'
on_press: root.parent.ContentSection.manager.current = 'home'
on_press: root.ContentSection.manager.current = 'home'
我覺得它是S-應對的問題,錯誤說這樣的話:
AttributeError: 'FooterSection' object has no attribute 'ContentSection'
所以我的應用程序有以下層次:
FloatLayout
HeaderSection
ContentSection
ScreenManager
FirstScreen
SecondScreen
ThirdScreen
FooterSection
Button for FirstScreen
Button for SecondScreen
Button for ThirdScreen
所以我需要遍歷一個級別到FloatLayout,然後深入到ContentSection訪問屏幕經理。
這工作,我也有類似的方法之前工作,但所有我的kivy代碼在我的python文件中。問題:爲什麼它是'root.ids.manager''ids'代表什麼? –
'.ids'是根部件存儲它的ID的地方。 – ebuenger