Kv file root widget

125 views
Skip to first unread message

Juan Francisco López-Egea Martínez

unread,
Feb 8, 2019, 5:44:01 AM2/8/19
to Kivy users support
Hello to the forum.

I've been doing some tests trying to understand how Kivy works, and in docs I've read about in Kv file, the root widget declaration don't use the "<>" symbols, here: https://kivy.org/doc/stable/guide/lang.html in "Rule context" part.

So, If I do things as are documented, it doesn't works for me, in the example I provide. What am I doing wrong?:

(This is just a label being modified in text and color, the important question is in .kv file). Just ready to comment/uncomment.

main.py:

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty
from kivy.clock import Clock
from colorsys import hsv_to_rgb, rgb_to_hsv
from kivy.uix.gridlayout import GridLayout

class MoneyControl(Widget):
    cartel
= ObjectProperty(None)

    lay
= 3
    rainbow
= [0.0,1.0,1.0,1.0]

   
def hsva_to_rgba(self, hsva):
        rgba
= hsv_to_rgb(hsva[0], hsva[1], hsva[2])
        rgba
= list(rgba)
        rgba
.append(hsva[3])
       
return rgba

   
def update(self, dt):
       
self.lay += 1
       
self.cartel.text = str(self.lay)
       
self.rainbow[0] += self.lay*0.0001
        finalRgba
= self.hsva_to_rgba(self.rainbow)
       
self.cartel.color = finalRgba


class MoneyControlApp(App):
   
def build(self):
        prog
= MoneyControl()
       
Clock.schedule_interval(prog.update, 1.0 / 60.0)
       
return prog

if __name__ == '__main__':
   
MoneyControlApp().run()


The .kv file:

#:kivy 1.0.9

<MoneyControl@Widget>: #This works. Comment this line...
#MoneyControl:#RootWidget # ... and uncomment this one. It stops working.
    cartel
: crt

   
Label:
        id
: crt
        font_size
: 20
        center_x
: root.width * 0.5
        top
: root.height * 0.5
        text
: "hola"
        color
: 1, .3, .8, 1



ZenCODE

unread,
Feb 8, 2019, 9:14:32 AM2/8/19
to Kivy users support
The "<MoneyControl>" rule (note that you don't need the @Widget because you do that in your Python file) defines a class rule that is applied to classes when you instantiate them. It does not instantiate anything by itself.

When you use "MoneyControl:", it declares an instance to which properties are applied. It does not define rules for all classes of that instance, only the one defined there. When you use this syntax, the properties defined in kv will not affect MoneyControl instances anywhere else. What use it then? If you went:

from kivy.lang import Builder
money_control
= Builder.load_file(moneyapp.kv)

money_control would then be an instance of MoneyControl with those properties applied. It's the root widget in the kv file, so it is returned when loading the kv file. I hope that makes sense?




Juan Francisco López-Egea Martínez

unread,
Feb 8, 2019, 5:27:30 PM2/8/19
to Kivy users support
Yes, I understand the difference, but... why in this case, it works when moneycontrol is a class, and not when is an instace?.

...and, why it works when <MoneyControl> is just a class definition, and not the instance that "roots" all the other widgets? (If anybody creates an instance of it)


Thank you for your time and support ZenCODE, really, at the very beginning, all is a little bit hard till something makes "click" in the head and everything gets instictive.

ZenCODE

unread,
Feb 9, 2019, 1:09:53 AM2/9/19
to Kivy users support
They both "work", they are perhaps just not doing what you expect. "<MoneyControl>:" creates a rule. "MoneyConrol:" creates an instance. What exactly is happening that you don't expect?

Juan Francisco López-Egea Martínez

unread,
Feb 9, 2019, 3:52:28 AM2/9/19
to Kivy users support
With a class definition, a label gets its color and text modified.

With an instance to be the root widget, an error arises about the line "self.cartel.text = str(self.lay)"
It says:

AttributeError: 'Nonetype' object has no attribute 'text'


I'm using Kivy v1.9.0 on Geany in Ubuntu, for if that helps...

It work to you?, The same code in you computer, nevermind which line gets commented?

Message has been deleted

ZenCODE

unread,
Feb 9, 2019, 4:39:33 AM2/9/19
to Kivy users support
Are you sure the kv file is being loaded? What is it's name? To double check, use the "Builder.load_file(your_file_name.kv)"....

Juan Francisco López-Egea Martínez

unread,
Feb 10, 2019, 8:02:31 AM2/10/19
to Kivy users support
Yes, of course I'm sure. If it wasn't in this way, it wouldn't work when I uncomment "<MoneyControl>" (class version) in .kv file...
Reply all
Reply to author
Forward
0 new messages