Fix some bugs when exporting composition to Kv language
authorJavier Sancho <jsf@jsancho.org>
Wed, 25 Jul 2018 17:32:50 +0000 (19:32 +0200)
committerJavier Sancho <jsf@jsancho.org>
Wed, 25 Jul 2018 17:32:50 +0000 (19:32 +0200)
kivyforms/formcanvas.py

index a1b7058..073587c 100644 (file)
@@ -201,18 +201,19 @@ class FormCanvas(BoxLayout):
             spacing=self._canvas.spacing[0]
         )
 
-    def export_to_kv(self, widget, indent=''):
+    def export_to_kv(self):
         kv = """StackLayout:
     orientation: '{orientation}'
     padding: {padding}
     spacing: {spacing}
-""".format(orientation=self.orientation, padding=self.padding, spacing=self.spacing)
+""".format(orientation=self._canvas.orientation, padding=self._canvas.padding, spacing=self._canvas.spacing)
 
         indent = '    '
-        stack = [self]
+        stack = [self._canvas]
 
         widgets = self.walk(restrict=True)
         next(widgets)    # the first widget is the FormCanvas
+        next(widgets)    # and the second is the inner StackLayout
         for widget in widgets:
             if not isinstance(widget, Grabbable):
                 # Look for the widget position inside the tree
@@ -228,9 +229,12 @@ class FormCanvas(BoxLayout):
                 stack.append(widget)
 
                 # Widget attributes
-                kv += """{indent}height: 40
-{indent}size_hint: (1., None)
-{indent}text: '{text}'
-""".format(indent=indent*len(stack), text=widget.text)
+                for attr in ('height', 'size_hint', 'text'):
+                    if hasattr(widget, attr):
+                        kv += "{indent}{attr}: {value}\n".format(
+                            indent=indent*len(stack),
+                            attr=attr,
+                            value=getattr(widget, attr)
+                        )
 
         return kv