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:
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_widget2:
Button:
text: 'bla'
:
my_widget:
Button:
text: 'bad'
:
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_widget2:
Button:
text: 'bla'
:
My_widget:
Button:
text: 'bad'
:
rows: 3
ToggleButton:
Image:
Label:
and there's one more visible widget!
3) a working layout with all stuff (my_widget2
-> My_widget2
)
:
My_widget2:
My_widget2:
Button:
text: 'bla'
:
My_widget:
Button:
text: 'bad'
:
rows: 3
ToggleButton:
Image:
Label:
Also to answer passing arguments into such widgets (
), use Factory to access such widget and then it's only passing (kw)args:
#:import Factory kivy.factory.Factory
:
Button:
on_release: root.add_widget(Factory.My_widget2(text='hi'))
:
size_hint: [None, None]
size: [200, 50]