Commits

Dusty Phillips  committed 71cf7f4

Demonstrate how it's possible to replace a widget with another

  • Participants
  • Parent commits 92880b0

Comments (0)

Files changed (2)

File kivycatalog.kv

 <Catalog>:
     language_box: language_box
     BoxLayout:
-        spacing: 30
+        spacing: 5
         TabbedPanel:
             size_hint: .6, 1
             tab_pos: "left_top"
                 AccordionItem:
                     on_collapse: root.show_kv(*args)
                     title: "BoxLayout"
-                    # I suppose it's possible to introspect this from the class name, but explicit is better than implicit
+                    # I suppose it's possible to introspect this from the class name
                     kv_file: "boxlayoutcontainer.kv"
+                    kv_container: boxlayoutcontainer
                     BoxLayoutContainer:
+                        id: boxlayoutcontainer
                 AccordionItem:
                     title: "Introduction"
                     Label:
                         text_size: self.width-200, 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.'''
-        Label:
+
+        BoxLayout:
+            id: bl
+            orientation: "vertical"
             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"    
+            Label:
+                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"
+            Button:
+                size_hint: 1, None
+                height: 50
+                text: "Update"
+                on_touch_down: root.change_kv(*args)
+
 from kivy.app import App
 from kivy.uix.boxlayout import BoxLayout
 from kivy.factory import Factory
-from kivy.lang import Builder
+from kivy.lang import Builder, Parser
 from kivy.properties import ObjectProperty
 
 Builder.load_file('boxlayoutcontainer.kv')
 class Catalog(BoxLayout):
     language_box = ObjectProperty()
 
+    def __init__(self, **kwargs):
+        super(Catalog, self).__init__(**kwargs)
+        self.kv_container = None
+
     def show_kv(self, object, collapsed):
         if not collapsed and hasattr(object, "kv_file"):
             with open(object.kv_file) as file:
                 self.language_box.text = file.read()
+                self.kv_container = object.kv_container
         else:
             self.language_box.text = ""
+            self.kv_container = None
+
+    def change_kv(self, object, event):
+        if self.kv_container:
+            self.kv_container.clear_widgets()
+            new_content = """#:kivy 1.4
+
+BoxLayout:
+    orientation: 'vertical'
+    padding: 20
+    spacing: 10
+    Button:
+        text: "EVERYBODY"
+    Button:
+        text: "LIKES"
+    Button:
+        text: "YOU"
+"""
+            parser = Parser(content=new_content)
+            Builder._apply_rule(self.kv_container, parser.root, parser.root)
 
 
 class KivyCatalogApp(App):