2017-03-05 43 views
0

我想從一個單獨的類中的按鈕控制屏幕管理器,但我無法弄清楚按鈕on_press:聲明上設置什麼。Python的基維屏幕管理器的wiget範圍

Kivy Python Nav

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訪問屏幕經理。

回答

1

導航窗口小部件樹對我來說一直很痛苦,AFAIK不能按照你喜歡的方式遍歷窗口小部件樹。

但是,您可以簡化您的小部件樹,確保所有內容都共享相同的根目錄並使用ids。

這裏是我做到了(我也動了一切千伏語言):

千伏

FloatLayout: 
    AnchorLayout: 
     anchor_x: 'center' 
     anchor_y: 'top' 
     Label: 
      size_hint: 1, .1 
      text: 'My App' 
    AnchorLayout: 
     anchor_x: 'center' 
     anchor_y: 'center' 
     ScreenManager: 
      id: manager 
      size_hint: 1, .8 
      Screen: 
       name: 'first' 
       Label: 
        text: 'First screen' 
      Screen: 
       name: 'second' 
       Label: 
        text: 'Second screen' 
      Screen: 
       name: 'third' 
       Label: 
        text: 'Third screen' 
    AnchorLayout: 
     anchor_x: 'center' 
     anchor_y: 'bottom' 
     BoxLayout: 
      orientation: 'horizontal' 
      size_hint: 1, .1 
      Button: 
       text: 'first' 
       on_press: root.ids.manager.current = 'first' 
      Button: 
       text: 'second' 
       on_press: root.ids.manager.current = 'second' 
      Button: 
       text: 'third' 
       on_press: root.ids.manager.current = 'third' 

蟒蛇

from kivy.app import App 
from kivy.lang import Builder 
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 



class MyAppApp(App): 
    def build(self): 
     return Builder.load_file('MyApp.kv') 


if __name__ == '__main__': 
    MyAppApp().run() 
+0

這工作,我也有類似的方法之前工作,但所有我的kivy代碼在我的python文件中。問題:爲什麼它是'root.ids.manager''ids'代表什麼? –

+0

'.ids'是根部件存儲它的ID的地方。 – ebuenger