Python, A class within a class, how to access variables from the upper class
<p>I have Qt <code>Gui</code> class that handles all the variables (<code>p1</code>, <code>p2</code>) adjustment with sliders and stuff. Inside this class, I have a OSC listener class that was supposed to listen to trigger signals and a variable <code>p3</code> from another device and use the parameters to trigger sound and graphic. But I have trouble accessing <code>p1</code>, <code>p2</code> in the listener class. Here is an example:</p>

<pre><code>class Ptsgui(QtGui.QMainWindow):
    def __init__(self):
        super(Ptsgui, self).__init__()
        self.p1, self.p2, self.data = 0, 0, np.zeros(10)
        self.initUI()

    class OscListener(object):
        def __init__(self, data):
            self.listenerData = data
            self.receive_address = '127.0.0.1', 7000
        def do_stuff_listener(self, addr, tags, stuff, source):
            print self.p1
            print self.p2
            self.p3 = stuff[0]
            trigger_sound_and_graphic(self.p1, self.p2, self.p3)

        def spawn(self):
            self.receiveServer = OSC.OSCServer(self.receive_address)
            self.receiveServer.addDefaultHandlers()
            self.receiveServer.addMsgHandler("/trigger", self.do_stuff_listener()
            self.emorating_oscServer = threading.Thread(target=self.receiveServer.serve_forever)
            self.emorating_oscServer.start()

    def initUI():
        """ 
        Some sliders setup for change the p1 &amp; p2
        """
        self.setGeometry(50, 50, 1050, 650)
        mainlayout = QtGui.QVBoxLayout()
        self.widget = QtGui.QWidget()
        self.widget.setLayout(mainlayout)
        self.listener = OscListener(data = self.data)
        self.show()
</code></pre>

<p>So here I want the <code>oscListener()</code> to be available to directly access <code>self.p1</code> and <code>self.p2</code>. And obviously I can't with this because the <code>self.p1</code>'s 'self' refers to <code>OscListener</code> but not <code>Ptsgui</code>. Also the <code>do_stuff_listener</code> is in a separate thread, is it still possible to access <code>self.p1</code> and <code>self.p2</code>? </p>

<p>Ultimately, I am hoping to the GUI for user to control the parameters values. And each time a trigger signal is received via <code>OSC</code>, it will generated a new graph and sound. Please advice if there is a better way to do this. </p>

<p>You could pass the<code>Ptsgui</code> to <code>OscListener</code> like this:</p>

<pre><code>class Ptsgui(QtGui.QMainWindow):
    def __init__(self):
        super(Ptsgui, self).__init__()
        self.p1, self.p2, self.data = 0, 0, np.zeros(10)
        self.initUI()

    class OscListener(object):
        def __init__(self, cls, data):
            self.parent = cls
            self.listenerData = data
            self.receive_address = '127.0.0.1', 7000
        def do_stuff_listener(self, addr, tags, stuff, source):
            print self.parent.p1 # Access it.
            print self.parent.p2
            self.p3 = stuff[0]
            trigger_sound_and_graphic(self.p1, self.p2, self.p3)

        def spawn(self):
            self.receiveServer = OSC.OSCServer(self.receive_address)
            self.receiveServer.addDefaultHandlers()
            self.receiveServer.addMsgHandler("/trigger", self.do_stuff_listener()
            self.emorating_oscServer = threading.Thread(target=self.receiveServer.serve_forever)
            self.emorating_oscServer.start()

    def initUI():
        """ 
        Some sliders setup for change the p1 &amp; p2
        """
        self.setGeometry(50, 50, 1050, 650)
        mainlayout = QtGui.QVBoxLayout()
        self.widget = QtGui.QWidget()
        self.widget.setLayout(mainlayout)
        self.listener = OscListener(cls=self, data = self.data) # Pass it here
        self.show()
</code></pre>

python class pyqt subclass osc