Python:关闭QMainWindow对话框后如何接近(或退出)Python进程
这里有两个.py文件,它们放在同一个文件夹里:
dialog_01.py and dialog_02.py
这两个文件是完全一样的。它们都创建了一个简单的QMainWindow对话框,里面有两个按钮:'确定'和'取消'。
点击'确定'按钮会关闭当前打开的对话框,并打开另一个对话框。所以如果点击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
这里是一个可以运行的例子。
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_())
2
重写你的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文件中去掉这些就解决了问题。