kivy custom widget initialization and __init__()

1,056 views
Skip to first unread message

mse...@unito.it

unread,
Dec 3, 2015, 3:36:12 AM12/3/15
to Kivy users support
Hi All,

I am running the code fragments in the docs, in particular the last example of:


In the example , a Custom Widget is created, and defined in a '.kv' file  (I show it at the bottom of the post,
but its specific content does not seem relevant

fragment of python code:

...........
class Controller(FloatLayout):
    '''Create a controller that receives a custom widget from the kv lang file.

    Add an action to be called from the kv lang file.
    '''
    label_wid = ObjectProperty()
    info = StringProperty()

    def do_action(self):
        self.label_wid.text = 'My label after button press'
        self.info = 'New info text'


If I run the example, everything is fine, but f I add a method __init__() for the widget as:

def __init__(self, **kwargs):
#basically do nothing
        super(FloatLayout, self).__init__(**kwargs)

The window is created, but the button and label are not resized.
What is going on here? What  special operations are performed by kivy?
 What is the correct way of implementing __init__() for a custom widget?


Kv file:

#:kivy 1.0

<Controller>:
    label_wid: my_custom_label

    BoxLayout:
        orientation: 'vertical'
        padding: 20

        Button:
            text: 'My controller info is: ' + root.info
            on_press: root.do_action()

        Label:
            id: my_custom_label
            text: 'My label before button press'

ZenCODE

unread,
Dec 3, 2015, 4:14:25 AM12/3/15
to Kivy users support
You  need to super the Controller, not the FloatLayout. Doing a super on the FloatLayout bypasses all of it's logic....

def __init__(self, **kwargs):
        super(Controller, self).__init__(**kwargs)

mse...@unito.it

unread,
Dec 3, 2015, 5:03:11 AM12/3/15
to Kivy users support
You are right! Sorry I was tired..

ZenCODE

unread,
Dec 3, 2015, 5:09:40 AM12/3/15
to Kivy users support
Coolness. No worries. I've done many dummer things myself :-)
Reply all
Reply to author
Forward
0 new messages