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 aglobal
, or was completely ignored when run through the language parser.Let's see:
1) your kv layout put into App:
The only visible thing will be a
Button
with'bad'
string2) a little change in naming -
my_widget
->My_widget
and there's one more visible widget!
3) a working layout with all stuff (
my_widget2
->My_widget2
)Also to answer passing arguments into such widgets (
<My@Widget>
), useFactory
to access such widget and then it's only passing (kw)args: