Commits

Dusty Phillips committed 5b2091a

Displaying the first ever .kv file alongside it's display

Comments (0)

Files changed (3)

boxlayoutcontainer.kv

+#:kivy 1.4
+
+<BoxLayoutContainer>:
+    BoxLayout:
+        orientation: 'vertical'
+        Button:
+            text: "hello"
+        Label:
+            text: "world"
+        Button:
+            text: "goodbye"
 #:kivy 1.4
 
 
-BoxLayout:
-    spacing: 30
-    TabbedPanel:
-        size_hint: .6, 1
-        tab_pos: "left_top"
-        default_tab_text: "Introduction"
-        default_tab_content: introduction_tab
+<Catalog>:
+    language_box: language_box
+    BoxLayout:
+        spacing: 30
+        TabbedPanel:
+            size_hint: .6, 1
+            tab_pos: "left_top"
+            default_tab_text: "Introduction"
+            default_tab_content: introduction_tab
 
-        Label:
-            id: introduction_tab
-            text: "A super awesome catalog of kivy widgets"
+            Label:
+                id: introduction_tab
+                text: "A super awesome catalog of kivy widgets"
 
-        TabbedPanelHeader:
-            text: 'Layouts'
-            content: layout_tab
+            TabbedPanelHeader:
+                text: 'Layouts'
+                content: layout_tab
 
-        Accordion:
-            id: layout_tab
-            AccordionItem:
-                title: "Introduction"
-                Label:
-                    text_size: self.width - 20, None
-                    markup: True
-                    text: '''Layouts are containers that automatically position their child widgets according to certain layout rules.\n\n[b]Anchor Layout[/b] aligns children to a border\n[b]Box Layout[/b] arranges children in a vertical or horizontal box.\n[b]Float layout[/b] places boxes according to their pos and size hints\n[b]Grid layout[/b] arranges the children in a matrix\n[b]Stack layout[/b] arranges children vertically or horizontally as far as they will fit.'''
-            AccordionItem:
-                title: "BoxLayout"
-                BoxLayoutContainer:
-    Label:
-        size_hint: .4, 1
-        id: "language_box"
-        text: "This box will display the kivy language for whatever has been selected"    
+            Accordion:
+                id: layout_tab
+                AccordionItem:
+                    title: "Introduction"
+                    Label:
+                        text_size: self.width, None
+                        markup: True
+                        text: '''Layouts are containers that automatically position their child widgets according to certain layout rules.\n\n[b]Anchor Layout[/b] aligns children to a border\n[b]Box Layout[/b] arranges children in a vertical or horizontal box.\n[b]Float layout[/b] places boxes according to their pos and size hints\n[b]Grid layout[/b] arranges the children in a matrix\n[b]Stack layout[/b] arranges children vertically or horizontally as far as they will fit.'''
+                AccordionItem:
+                    on_collapse: root.show_kv(args)
+                    title: "BoxLayout"
+                    kv_file: "boxlayoutcontainer.kv"
+                    BoxLayoutContainer:
+                AccordionItem:
+                    Label:
+                        text: "something"
+        Label:
+            size_hint: .4, 1
+            text_size: self.width-20, self.height-20
+            valign: "top"
+            id: language_box
+            text: "This box will display the kivy language for whatever has been selected"    
 from kivy.app import App
-from kivy.uix.button import Button
-from kivy.uix.widget import Widget
+from kivy.uix.boxlayout import BoxLayout
 from kivy.factory import Factory
+from kivy.lang import Builder
+from kivy.properties import ObjectProperty
 
+Builder.load_file('boxlayoutcontainer.kv')
 
-class BoxLayoutContainer(Widget):
-    def __init__(self, **kwargs):
-        super(BoxLayoutContainer, self).__init__(**kwargs)
-        # why is there no parent
-        #parent = self.get_parent_window()
-        #self.height = parent.height
-        #self.width = parent.width
 
-        # why is size 100, 100?
-        print self.size
-        self.height = 400
-        self.width = 400
+class BoxLayoutContainer(BoxLayout):
+    pass
+
 
-        # why does this widget not display?
-        self.add_widget(Button(text="hello"))
+class Catalog(BoxLayout):
+    language_box = ObjectProperty()
 
-        #why does this widget display only with the sizes set above?
-        self.add_widget(Button(text="bonjour", center=self.center))
+    def show_kv(self, event):
+        object, collapsed = event
+        if not collapsed and hasattr(object, "kv_file"):
+            with open(object.kv_file) as file:
+                self.language_box.text = file.read()
+        else:
+            self.language_box.text = ""
 
 
 class KivyCatalogApp(App):
-    pass
+    def build(self):
+        return Catalog()
 
 for cls in (BoxLayoutContainer,):
     Factory.register(cls.__name__, cls)