2017-03-05 3 views
0

Ich versuche, einen Bildschirm-Manager von Schaltflächen in einer separaten Klasse zu steuern, aber ich kann nicht herausfinden, was auf der Schaltfläche on_press: Anweisungen gesetzt werden.Python Kivy Screen Manager wiget Bereich

Kivy Python Nav

Kivy Datei:

<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-Datei:

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() 

habe ich versucht, verschiedene Methoden:

on_press: root.parent.ContentSection.ScreenManager.current = 'home' 
on_press: root.parent.ContentSection.manager.current = 'home' 
on_press: root.ContentSection.manager.current = 'home' 

Ich mag es fühlen, ist ein s Bewältigung Problem, sagen Fehler Dinge wie:

AttributeError: 'FooterSection' object has no attribute 'ContentSection' 

Also meine App die folgende Hierarchie:

FloatLayout 
    HeaderSection 
    ContentSection 
     ScreenManager 
      FirstScreen 
      SecondScreen 
      ThirdScreen 
    FooterSection 
     Button for FirstScreen 
     Button for SecondScreen 
     Button for ThirdScreen 

Also muss ich eine Ebene in FloatLayout durchqueren, dann Drilldown in ContentSection den Bildschirm zugreifen Manager.

Antwort

1

Das Navigieren von Widget-Bäumen war für mich ein Schmerz, und AFAIK können Sie den Widget-Baum nicht so durchqueren, wie Sie möchten.

Sie können jedoch Ihren Widget-Baum vereinfachen, stellen Sie sicher, dass alles denselben Stamm hat und verwenden Sie IDs.

Hier ist, wie ich es getan hätte (ich zog auch alle kv Sprache):

kv

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' 

Python

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

Dies funktioniert, hatte ich ein ähnliches Verfahren zu arbeiten, bevor aber all Mein Kivy-Code war in meiner Python-Datei. Frage: Warum steht der 'root.ids.manager' für was steht der 'ids'? –

+0

'.ids' ist, wo ein Root-Widget seine IDs speichert. – ebuenger