Python:QMainWindow对话框关闭后如何关闭(或退出)Python进程

时间:2014-03-17 20:50:39

标签: python pyqt pyqt4 qmainwindow

有两个.py文件放在同一个文件夹中:

dialog_01.py and dialog_02.py

两个文件都是彼此的副本。两者都构建了一个简单的QMainWindow对话窗口,其中包含两个按钮:' Ok'和'取消'。

点击'确定'按钮关闭当前打开的对话窗口并打开另一个窗口。因此,如果单击Dialog_01的确定按钮,则关闭dialog_01并打开dialog_02。如果单击Dialog_02的确定按钮,则关闭dialog_02并打开dialog_01,依此类推。

编辑问题:

关闭对话框会使Python进程仍然在后台运行(可以在OSX活动监视器或Windows任务管理器中看到)。

如何在对话窗口关闭后确保Python进程终止?

dialog_01.py

import sys, os
from PyQt4 import QtCore, QtGui    
if 'app' not in globals().keys(): app = QtGui.QApplication(sys.argv)

class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()       

        Cancel_button = QtGui.QPushButton("Cancel")
        Cancel_button.clicked.connect(self.Cancel)      
        myBoxLayout.addWidget(Cancel_button) 


        Button_01 = QtGui.QPushButton("Press to close this dialog and open Dialog 02")
        Button_01.clicked.connect(self.callAnotherQMainWindow)
        myBoxLayout.addWidget(Button_01)        

        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)
        self.setWindowTitle('Dialog 01')

    def callAnotherQMainWindow(self):
        from dialog_02 import Dialog_02
        self.close()
        self.dialog_02 = Dialog_02()
        self.dialog_02.show()
        self.dialog_02.raise_()

    def Cancel(self):
        self.close()

    def closeEvent(self):
        self.deleteLater()



if __name__ == '__main__':
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(480,320)
    sys.exit(app.exec_())

dialog_02.py

import sys, os
from PyQt4 import QtCore, QtGui    
if 'app' not in globals().keys(): app = QtGui.QApplication(sys.argv)

class Dialog_02(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()

        Cancel_button = QtGui.QPushButton("Cancel")
        Cancel_button.clicked.connect(self.Cancel)      
        myBoxLayout.addWidget(Cancel_button) 


        Button_02 = QtGui.QPushButton("Press to close this dialog 02 and re-open Dialog 01")
        Button_02.clicked.connect(self.callAnotherQMainWindow)
        myBoxLayout.addWidget(Button_02)

        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)
        self.setWindowTitle('Dialog 02')

    def callAnotherQMainWindow(self):
        from dialog_01 import Dialog_01
        self.close()
        self.dialog_01 = Dialog_01()
        self.dialog_01.show()
        self.dialog_01.raise_()

    def Cancel(self):
        self.close()

    def closeEvent(self):
        self.deleteLater()


if __name__ == '__main__':
    dialog_2 = Dialog_02()
    dialog_2.show()
    dialog_2.resize(480,320)
    sys.exit(app.exec_())

2 个答案:

答案 0 :(得分:1)

覆盖QtGui.QMainWindow类的closeEvent()函数,如下所示:

def closeEvent(self, event):
    self.deleteLater()

这似乎有效:

删除此内容:

if 'app' not in globals().keys(): app = QtGui.QApplication(sys.argv)

和此:

if __name__ == '__main__':
    dialog_2 = Dialog_02()
    dialog_2.show()
    dialog_2.resize(480,320)
    sys.exit(app.exec_())

...来自dialog2文件就可以了。

答案 1 :(得分:0)

以下是工作示例。

dialog_01.py

import sys, os
from PyQt4 import QtCore, QtGui  

class Dialog_01(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()       

        Cancel_button = QtGui.QPushButton("Cancel")
        Cancel_button.clicked.connect(self.Cancel)      
        myBoxLayout.addWidget(Cancel_button) 


        Button_01 = QtGui.QPushButton("Press to close this dialog and open Dialog 02")
        Button_01.clicked.connect(self.callAnotherQMainWindow)
        myBoxLayout.addWidget(Button_01)        

        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)
        self.setWindowTitle('Dialog 01')

    def callAnotherQMainWindow(self):
        from dialog_02 import Dialog_02
        self.close()
        self.dialog_02 = Dialog_02()
        self.dialog_02.show()
        self.dialog_02.raise_()

    def Cancel(self):
        self.close()

    # def closeEvent(self, event):
    #     self.deleteLater()



if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_1 = Dialog_01()
    dialog_1.show()
    dialog_1.resize(480,320)
    sys.exit(app.exec_())

dialog_02.py

import sys, os
from PyQt4 import QtCore, QtGui    

class Dialog_02(QtGui.QMainWindow):
    def __init__(self):
        super(QtGui.QMainWindow,self).__init__()

        myQWidget = QtGui.QWidget()
        myBoxLayout = QtGui.QVBoxLayout()

        Cancel_button = QtGui.QPushButton("Cancel")
        Cancel_button.clicked.connect(self.Cancel)      
        myBoxLayout.addWidget(Cancel_button) 


        Button_02 = QtGui.QPushButton("Press to close this dialog 02 and re-open Dialog 01")
        Button_02.clicked.connect(self.callAnotherQMainWindow)
        myBoxLayout.addWidget(Button_02)

        myQWidget.setLayout(myBoxLayout)
        self.setCentralWidget(myQWidget)
        self.setWindowTitle('Dialog 02')

    def callAnotherQMainWindow(self):
        from dialog_01 import Dialog_01
        self.close()
        self.dialog_01 = Dialog_01()
        self.dialog_01.show()
        self.dialog_01.raise_()

    def Cancel(self):
        self.close()

    # def closeEvent(self, event):
    #     self.deleteLater()


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    dialog_2 = Dialog_02()
    dialog_2.show()
    dialog_2.resize(480,320)
    sys.exit(app.exec_())