MainWindow中的值从childWindow返回

时间:2014-03-06 07:08:31

标签: python qt childwindow qmainwindow

关闭ChildWindow后,如何将ChildWindow中的值发送到MainWindow?

当按下ChildWindow中的“closeButton”时,ChildWindow将计算出的值发送给MainWindow。

我做了这样的测试:

def closeEvent(self, evnt):
        value = 34
        return value

它没有用,我得到了这个:

TypeError: invalid result type from ChildWindow.closeEvent()

1 个答案:

答案 0 :(得分:1)

以下是如何做到这一点的简单示例:

import sys
from PyQt4 import QtGui


class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)

        self.button = QtGui.QPushButton("Make child")
        self.button.clicked.connect(self.openChildWindow)

        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.button)

    def openChildWindow(self):
        self.childWindow = ChildWindow(self)
        self.childWindow.setGeometry(650, 350, 200, 300)
        self.childWindow.show()

    def printResult(self, value):  
        print value

class ChildWindow(Window):
    def __init__(self, parentWindow):
        super(ChildWindow, self).__init__()
        self.parentWindow = parentWindow
        self.parentWindow.printResult("I'm in child init")
        self.button.setDisabled(1)

    def closeEvent(self, event):
        self.parentWindow.printResult("Closing child, this text is returned to parent")

if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(600, 300, 200, 300)
    window.show()
    sys.exit(app.exec_())