I try to understand how kivy .kv files work, so I created a small application with a horizontial BoxLayout which contains three GridLayouts, as shown:
my_widget:
<my_widget@BoxLayout>:
orientation: "horizontal"
GridLayout:
rows: 3
ToggleButton:
Image:
Label:
GridLayout:
rows: 3
ToggleButton:
Image:
Label:
GridLayout:
rows: 3
ToggleButton:
Image:
Label:
No problem there, but since there are same blocks of widgets (GridLayouts) could they be duplicated? I tried something like: https://kivy.org/docs/api-kivy.lang.html
my_widget:
[my_widget2@GridLayout]:
rows: 3
ToggleButton:
Image:
Label:
<my_widget@BoxLayout>:
orientation: "horizontal"
my_widget2:
my_widget2:
my_widget2:
but didn't work. If duplicating is possible, then how can I pass information to each one widget in the block?
Yeah, this gets me badly everytime I encounter it e.g. copy&paste word into <>
(and forget about uppercase). I still wonder if I should treat it as a bug or as a feature, because it forces user to name widgets in a correct style/case, which makes it easier for reading too.
The thing is that widgets/rules in kv language should use ThisWordStyle or something similar, but the first capital letter seems to be important. I encountered even a case when a capital letter inside the word was enough and the rest lowercase, but can't reproduce, sadly.
Words with lowercase only are mostly used as properties or variables, so maybe the my_widget
was handled as a property or variable too like a global
, or was completely ignored when run through the language parser.
Let's see:
1) your kv layout put into App:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
Builder.load_string('''
<my_widget@BoxLayout>:
my_widget2:
Button:
text: 'bla'
<MyWidget>:
my_widget:
Button:
text: 'bad'
<my_widget2@GridLayout>:
rows: 3
ToggleButton:
Image:
Label:
''')
class MyWidget(BoxLayout):
pass
class Test(App):
def build(self):
return MyWidget()
Test().run()
The only visible thing will be a Button
with 'bad'
string
2) a little change in naming - my_widget
-> My_widget
<My_widget@BoxLayout>:
my_widget2:
Button:
text: 'bla'
<MyWidget>:
My_widget:
Button:
text: 'bad'
<my_widget2@GridLayout>:
rows: 3
ToggleButton:
Image:
Label:
and there's one more visible widget!
3) a working layout with all stuff (my_widget2
-> My_widget2
)
<My_widget@BoxLayout>:
My_widget2:
My_widget2:
Button:
text: 'bla'
<MyWidget>:
My_widget:
Button:
text: 'bad'
<My_widget2@GridLayout>:
rows: 3
ToggleButton:
Image:
Label:
Also to answer passing arguments into such widgets (<My@Widget>
), use Factory
to access such widget and then it's only passing (kw)args:
#:import Factory kivy.factory.Factory
<MyWidget>:
Button:
on_release: root.add_widget(Factory.My_widget2(text='hi'))
<My_widget2@Label>:
size_hint: [None, None]
size: [200, 50]