community
community copied to clipboard
Documentation of the load_kv() method of kivy.App is incorrect
Software Versions
- Python: Any
- OS: Any
- Kivy: 2.3.0.dev0
- Kivy installation method: Any
Describe the bug The current documentation states:
This method will search for a file named showcase.kv in the directory that contains main.py. The name of the kv file has to be the lowercase name of the class, without the ‘App’ postfix at the end if it exists.
This is incorrect, the name of the kv file is the lowercase name of the class, with or without the ‘App’ postfix.
Expected behavior I expect the documentation to be correct
To Reproduce run the following code and note that the kv file is automatically loaded by the kivy.App code. python file:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
class MyGameScreen(BoxLayout):
def __init__(self):
super(MyGameScreen, self).__init__()
self.i = 0
def btn_push_press(self):
if self.i == 0:
self.btn_push.back_color = (0, 0, 1, 1)
self.btn_push.pressed_color = (1, 0, 0, 1)
self.i = 1
elif self.i == 1:
self.btn_push.back_color = (0, 1, 1, 1)
self.btn_push.pressed_color = (1, 0, 1, 1)
self.i = 0
class MyCoolApp(App):
def build(self):
return MyGameScreen()
if __name__ == '__main__':
MyCoolApp().run()
kv file named "mycoolapp.kv":
<MyGameScreen>:
btn_push: btn_push
BoxLayout:
id: game_screen
orientation: 'vertical'
MyRoundedButton_push:
id: btn_push
text: "PUSH"
font_size: 48
color: [1,1,1,1]
on_press: root.btn_push_press()
<MyRoundedButton_push@Button>:
background_normal: ''
background_color: (0, 0, 0, 0)
back_color: (0, 1, 1, 1)
pressed_color: (1, 0, 1, 1)
border_radius: [100]
canvas.before:
Color:
rgb: self.back_color if self.state == 'normal' else self.pressed_color
RoundedRectangle:
size: self.size
pos: self.pos
radius: self.border_radius
Additional context Add any other context about the problem here.
Confirming that yes, this is the behaviour due to this code:
https://github.com/kivy/kivy/blob/26456cc7e2cd8e2100df45ccca26e60cee85b213/kivy/app.py#L685-L689
We could change the behaviour (API-Break) or change the documentation.
Either way, this is very unlikely to cause confusion, so low priority.
Caused confusion for me!
Caused confusion for me!
Perhaps you can explain how? How did it come to be that you accidentally had an MyNameApp.kv as well as a MyName.kv file?
I could not get the directory example to work (load kv file from directory). Maybe it was because of this name issue.
I stand behind my analysis then.
The documentation says: If your application is called FooApp
, the file shall be called foo.kv
. If you application is called Foo
, the file shall be called foo.kv
.
The code says: If your application is called FooApp
the file shall be called fooapp.kv
but if that isn't present, the file shall be called foo.kv
. If you application is called Foo
, the file shall be called foo.kv
.
This means, if you follow the instructions, it works perfectly.
If you ignore the instructions, and call your file fooapp.kv
, it still works perfectly.
The only way it can cause confusion is if you decide to call your file foo.kv
and you decide to have a different file called fooapp.kv
in the same directory (which would be a terrible idea by any naming standards) and you couldn't figure out that the latter was taking priority over the former.
This is such an unlikely scenario, adding an explanation to the documentation may well be detrimental - causing more confusion than it removes. In that case, it may make sense to change the code to be simpler rather than document its behaviour.
I do not know why your example didn't work, but I am skeptical it was because of this code.
Thanks for the explanation. It must've been something else. I am not sure, maybe it was looking in the wrong directory.