Python,类中的类,如何从上层类

时间:2016-08-28 10:57:51

标签: python class pyqt subclass osc

我有Qt Gui类,用滑块和东西来处理所有变量(p1p2)。在这个类中,我有一个OSC监听器类,它应该从另一个设备监听触发信号和变量p3,并使用这些参数来触发声音和图形。但是我在侦听器类中访问p1p2时遇到问题。这是一个例子:

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 & 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()

所以我希望oscListener()可以直接访问self.p1self.p2。显然我不能用这个,因为self.p1的'self'指的是OscListener而不是Ptsgui。此外,do_stuff_listener位于单独的帖子中,是否仍然可以访问self.p1self.p2

最终,我希望GUI能够让用户控制参数值。每次通过OSC接收到触发信号时,它都会生成新的图形和声音。如果有更好的方法,请建议。

2 个答案:

答案 0 :(得分:0)

您可以将Ptsgui传递给OscListener,如下所示:

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 & 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()

答案 1 :(得分:0)

你想要实现的目标可以这样做:

def generate_array(m, n):
    return (np.arange(m) * 1j)[:, None] + np.arange(n)


generate_array(2, 3)
Out: 
array([[ 0.+0.j,  1.+0.j,  2.+0.j],
       [ 0.+1.j,  1.+1.j,  2.+1.j]])

请注意,不需要从代码示例中嵌套类。

另外,在我看来,你想要达到的目标是不对的。代码非常耦合。考虑将class Ptsgui(QtGui.QMainWindow): def __init__(self): super(Ptsgui, self).__init__() self.p1, self.p2, self.data = 0, 0, np.zeros(10) self.initUI() def initUI(): """ Some sliders setup for change the p1 & p2 """ self.setGeometry(50, 50, 1050, 650) mainlayout = QtGui.QVBoxLayout() self.widget = QtGui.QWidget() self.widget.setLayout(mainlayout) self.listener = OscListener(gui=self, data=self.data) self.show() class OscListener(object): def __init__(self, gui, data): self.gui = gui self.listenerData = data self.receive_address = '127.0.0.1', 7000 def do_stuff_listener(self, addr, tags, stuff, source): print self.gui.p1 print self.gui.p2 self.p3 = stuff[0] trigger_sound_and_graphic(self.gui.p1, self.gui.p2, self.gui.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() Ptsgui分离。检查Observer pattern以获得一些灵感。