Access kivy popup parent
<p>The way popups are implemented in kivy, the popup seems to get attached to the window and not the parent object which created the popup.  Popup comes with self.dismiss() to close the popup but I can't figure out any way to access the 'parent' object since despite creating the popup, it seems to exist outside of it.</p>

<p>Example snippets:</p>

<pre><code>class StartButton(ActionButton)
    def on_release(self):
        self.popup = StartPop(id='popid')
        self.popup.open()

class StartPop(Popup):
    def close(self):
        self.dismiss()
    def start(self):
        print(self)
        print(self.parent)
</code></pre>

<p>The result of the print commands is</p>

<pre><code>&lt;__main__.StartPop object at 0x00000000037BBCE0&gt;

&lt;kivy.core.window.window_sdl2.WindowSDL object at 0x000000000373B0B0&gt;
</code></pre>

<p>So rather than the parent being StartButton, whose parent I would also expect to access etc. the parent is the Window.  </p>

<p>I don't see how I could bind any function that thus interact with the widget I used to create the popup from.  I need to be able to get the parent object and its parents to do things based on what I click within the popup but I can't figure out how this could be implemented.</p>

<p>In the .kv file</p>

<pre><code>&lt;StartPop&gt;:
    title: 'Popup'
    auto_dismiss: True
    size_hint: None,None
    size: 400,250
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'sample text here'
            text_size: self.size
            halign: 'center'
            valign: 'middle'
        BoxLayout:
            orientation: 'horizontal'
            Button:
                size_hint: 1,0.5
                text: 'Cancel'
                on_release: root.close()
            Button:
                size_hint: 1,0.5
                text: 'Start Testing'
                on_release: root.start()
</code></pre>

<p>It's implemented like that because it needs to be hidden most of the time, yet still active, so that <code>open()</code> could be called. Kivy doesn't seem to handle hiding of the widgets other way that actually <em>removing</em> it and keeping a reference somewhere (there's no <code>hide</code> property), so maybe even because of that. Or because it was easier to implement it this way. It's not bad implementation however and the way OO programming works you can do some fancy stuff with it too. The thing you want can be handled simply with <code>kwargs</code> in <code>__init__</code>:</p>

<p>Inherit from Popup and get a custom keyword argument:</p>

<pre><code>class StartPop(Popup):
    def __init__(self, **kwargs):
        self.caller = kwargs.get('caller')
        super(StartPop, self).__init__(**kwargs)
        print self.caller
</code></pre>

<p>Then create an instance of that custom <code>Popup</code> and set the parent:</p>

<pre><code>pop = StartPop(caller=self)
pop.open()
</code></pre>

<p>The <code>caller</code> keyword isn't limited only to Kivy widgets. Put there any object you want to do stuff with and you can then access it inside the <code>StartPop</code> object via <code>self.caller</code></p>

python python-2.7 popup kivy