Attributes management in .kv file

30 views
Skip to first unread message

Ayowel Seireitei

unread,
May 13, 2017, 3:08:43 AM5/13/17
to kivy-...@googlegroups.com
Hi,

I'm fairly new to kivy, so I might just be doing things the wrong way, but I have what I tend to consider to be a bug even though it might not be the case.

Consider this kv file :

#:kivy 1.0

<MainMenuButton@Button>
  mycustomscale: 0
  #pos: (root.pos[0]), self.parent.height/2
  pos: (root.pos[0]) + (self.mycustomscale * 10), self.parent.height/2
 

Button:

  MainMenuButton:
    mycustomscale: 30
    text:'Top left'
    size: root.width / 5, root.height/3
    #center: root.center_x + root.width, root.center_y

Running this code produces a white screen with the main loop returning the following traceback :

   File "kivy\weakproxy.pyx", line 22, in kivy.weakproxy.WeakProxy.__setattr__ (kivy\weakproxy.c:1235)
   File "kivy\properties.pyx", line 408, in kivy.properties.Property.__set__ (kivy\properties.c:5114)
   File "kivy\properties.pyx", line 1268, in kivy.properties.ReferenceListProperty.set (kivy\properties.c:20384)
   File "kivy\properties.pyx", line 501, in kivy.properties.Property.dispatch (kivy\properties.c:6557)
   File "kivy\_event.pyx", line 1224, in kivy._event.EventObservers.dispatch (kivy\_event.c:13497)
   File "kivy\_event.pyx", line 1106, in kivy._event.EventObservers._dispatch (kivy\_event.c:12301)
   File "C:\Program Files (x86)\Python2\lib\site-packages\kivy\lang.py", line 1569, in call_fn
     setattr(element, key, e_value)

With the following error at startup :

   File "main.py", line 9, in <module>
     MainApp().run()
   File "C:\Program Files (x86)\Python2\lib\site-packages\kivy\app.py", line 801, in run
     self.load_kv(filename=self.kv_file)
   File "C:\Program Files (x86)\Python2\lib\site-packages\kivy\app.py", line 598, in load_kv
     root = Builder.load_file(rfilename)
   File "C:\Program Files (x86)\Python2\lib\site-packages\kivy\lang.py", line 1842, in load_file
     return self.load_string(data, **kwargs)
   File "C:\Program Files (x86)\Python2\lib\site-packages\kivy\lang.py", line 1921, in load_string
     self._apply_rule(widget, parser.root, parser.root)
   File "C:\Program Files (x86)\Python2\lib\site-packages\kivy\lang.py", line 2130, in _apply_rule
     e), cause=tb)
 kivy.lang.BuilderException: Parser: File ".\main.kv", line 12:
 ...
       10:
       11:  MainMenuButton:
>>   12:    mycustomscale: 30
       13:    text:'Top left'
       14:    size: root.width / 5, root.height/3
 ...
 RuntimeError: maximum recursion depth exceeded

So , the problem seems to be the fact that I make a new assignment to a custom variable when it has already been assigned and is used by the parent class. The problem doesn't occur if the variable is one of the default kivy variables, is used in the child class instead of the parent or is set in the child class but not used in the parent class.


I thought about sending an issue on github as it doesn't seem to have been reported yet, however I'd like to know wether this behavior is expected first.
Can someone tell me ?


I know that a simple workaround would be to use python classes instead of kv or to set the position in every child class instead of the parent, but that doesn't feel as 'clean' as what I'm trying to do and I'd really like to do it this way (if you know other ways I'd love to hear it though)

Sorry for the mistakes I have probably made, I'll try to improve my english in the future.

ps : I'm using kivy on windows

Matthew Einhorn

unread,
May 13, 2017, 10:41:02 AM5/13/17
to kivy-...@googlegroups.com
The recursion happens because you set the size as a function of it's size. You do the same for pos. This leads to an infinite recursion.

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ayowel Seireitei

unread,
May 13, 2017, 1:03:38 PM5/13/17
to Kivy users support
I might be mistaken, but I thought that root was a reference to the main object (the window), not the current object :/ . Isn't it the case ?
Moreover, this code produces no recursion if I use the pos commented instead of the one used right now (in <MainMenuButton@Button>), which points to a problem with mycustomscal, not root.pos.

Matthew Einhorn

unread,
May 13, 2017, 1:16:56 PM5/13/17
to kivy-...@googlegroups.com
In `pos: (root.pos[0]) + (self.mycustomscale * 10), self.parent.height/2` root is the same as self because there's only one widget declared in that rule. So you're basically saying there that the x pos should be the x pos plus some offset, which is circular and recursive.

On Sat, May 13, 2017 at 1:03 PM, Ayowel Seireitei <ayow...@gmail.com> wrote:
I might be mistaken, but I thought that root was a reference to the main object (the window), not the current object :/ . Isn't it the case ?
Moreover, this code produces no recursion if I use the pos commented instead of the one used right now (in <MainMenuButton@Button>), which points to a problem with mycustomscal, not root.pos.

--

Ayowel Seireitei

unread,
May 13, 2017, 1:58:47 PM5/13/17
to Kivy users support
Oh. I think I get it, Thank you.

I thought that the Button was considered as the root widget.
I will have to be careful with integration and properly test what is happening with my code.

Thank you again
Reply all
Reply to author
Forward
0 new messages