from kivy.app import App
from kivy.lang import Builder
class MainApp(App):
def build(self):
Builder.load_file("ui/other_widgets.kv")
return Builder.load_file("main.kv")
if __name__ == "__main__":
MainApp().run()
main.kv
#:kivy 2.0.0
#:include ui/other_widgets.kv
FloatLayout:
canvas:
Color:
rgb: 0, 1, 0
Rectangle:
pos: self.pos
size: self.size
OtherWidget:
size_hint: 0.5, 0.5
other_widgets.kv
#:kivy 2.0.0
<OtherWidget>:
canvas:
Color:
rgb: 0, 0, 1
Rectangle:
pos: self.pos
size: self.size
Label:
text: "Other Widget"What am I doing wrong?
There are a few things going on here.
In other_widgets.kv, other_widgets is not inheriting from a widget base class so it is not defined. Change to:
#:kivy 2.0.0
<OtherWidget@BoxLayout>:
canvas:
Color:
rgb: 0, 0, 1
Rectangle:
pos: self.pos
size: self.size
Label:
text: "Other Widget"
The you can decide either you include it in another file or you do a builder.load_file()
If you do builder.load_file, it must be done prior to build:
from kivy.app import App
from kivy.lang import
Builder
Builder.load_file('ui/other_widgets.kv') #loading file prior to build
class MainApp(App):
def build(self):
return Builder.load_file("main.kv")
if __name__ == "__main__":
MainApp().run()
and remove the include in main.kv:
#:kivy 2.0.0
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CA%2Bqqht4BgdYbvwU9uWmznQWVL4nXdAwVf-Bf%3DUYF7i8QRine9A%40mail.gmail.com.
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.properties import StringProperty
Builder.load_file("other_widgets.kv")
class OtherWidget(BoxLayout):
other_property = StringProperty("foobar")
#:kivy 2.0.0
<OtherWidget>: # I'm assuming kv lang knows OtherWidget is a BoxLayout from other_widgets.py?
canvas:
Color:
rgb: 0, 0, 1
Rectangle:
pos: self.pos
size: self.size
from kivy.app import App
from kivy.lang import Builder
Builder.load_file("ui/other_widgets.kv")
class MainApp(App):
def build(self):
return Builder.load_file("main.kv")
if __name__ == "__main__":
MainApp().run()
#:kivy 2.0.0
FloatLayout:
canvas:
Color:
rgb: .9, .9, .9
Rectangle:
pos: self.pos
size: self.size
OtherWidget:
id: other_widget
Label:
text: other_widget.some_property
In this case you would import the otherwidgets.py file into main.
from kivy.app import App
from kivy.lang import Builder
import ui.other_widgets
# Builder.load_file('ui/other_widgets.kv')
class MainApp(App):
def build(self):
return Builder.load_file("main.kv")
if __name__ == "__main__":
MainApp().run()
In the the other_widgets.py file, the ‘trick’ is the need to specify the ui directory in the load_file call. This code is being executed in the location it is being imported (main.py)
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.properties import
StringProperty
Builder.load_file("ui/other_widgets.kv")
class OtherWidget(BoxLayout):
other_property = StringProperty("foobar")
In these cases I often like to combine the python and kv code into one file. This removes the path dependency of the load_file call. from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.properties import
StringProperty
Builder.load_string("""
<OtherWidget@BoxLayout>:
canvas:
Color:
rgb: 0, 0, 1
Rectangle:
pos: self.pos
size: self.size
Label:
text: "Other Widget"
""")
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/34a92741-731f-496a-9439-6b6d21f38d82n%40googlegroups.com.