Kivy object doesn't render when defined in .kv file
<p>I'm developing an app in kivy and currently trying to figure out why I can get an object to render when I add it in Python but not in the .kv file.</p>

<p>This works fine, rendering the background image with a switch on top of it.</p>

<pre><code>class LoginScreen(Screen):
    def __init__(self,**kwargs):
        super(LoginScreen, self).__init__(**kwargs)
        self.layout = BoxLayout(orientation='vertical')
        self.add_widget(self.layout)
        self.layout.add_widget(defSwitch())
    def on_touch_up(self,touch):
        if self.collide_point(*touch.pos):
            self.parent.current = 'data'

class defSwitch(Switch):
    active = False
</code></pre>

<p>Where the .kv file is:</p>

<pre><code>&lt;LoginScreen&gt;:
    imgname: './images/' + 'login.jpg'
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: self.imgname
</code></pre>

<p>This doesn't render anything but the background image:</p>

<pre><code>class LoginScreen(Screen):
    def on_touch_up(self,touch):
        if self.collide_point(*touch.pos):
            self.parent.current = 'data'

class defSwitch(Switch):
    active = False
</code></pre>

<p>where in this case the .kv file has:</p>

<pre><code>&lt;LoginScreen&gt;:
    imgname: './images/' + 'login.jpg'
    canvas.before:
        Rectangle:
            pos: self.pos
            size: self.size
            source: self.imgname
    BoxLayout:
        orientation: 'vertical'
        defSwitch:
</code></pre>

<p>If I replace defSwitch: in the .kv file by a default Switch object, then it renders fine.  Why can't I use a custom object?</p>

<p>If you want to use a custom class created in <code>.py</code> file with kv language, you need to set it in <code>.kv</code> file too:</p>

<pre><code>&lt;defScreen&gt;:    # class def

&lt;LoginScreen&gt;:
    ...
    defScreen:  # object
</code></pre>

<p>so that it knows what to look for.</p>

<p>Also I'd like to recommend you using camel case in kv language as there are <a href="http://stackoverflow.com/a/40030291/5994041">situations</a> in which the parser won't recognize the phrase as a widget.</p>

python python-2.7 kivy python-2.x kivy-language