通过模态对话框关闭 PyQt 应用程序
Close PyQt App via a Modal Dialog
我目前正在使用 Qt4 python 绑定编写应用程序,该绑定要求用户提供登录凭据。在我的应用程序启动时,我会显示一个模式对话框,用户可以在其中输入他们的数据。由于应用程序在用户未登录时无法提供有用的服务,所以我想在用户单击取消按钮时关闭应用程序。
因此,如果对话框 returns 的结果是否定的,我只需调用 QMainWindow
的 close()
方法。通常这会导致应用程序退出,因为不再有可交互的 windows。
但是,如果以前显示过模态对话框,应用程序只会继续 运行,我必须手动将其终止。
下面是一个最小示例的代码
main.py:
import sys
from PyQt4.QtGui import QApplication
from MyMainWindow import MyMainWindow
app = QApplication(sys.argv)
window = MyMainWindow()
window.show()
app.exec_()
print "Terminated"
MyMainWindow.py:
from PyQt4.QtGui import QMainWindow
from PyQt4 import QtGui
from MyDialog import MyDialog
class MyMainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setWindowTitle("Close App Test")
self.centralwidget = QtGui.QWidget(self)
self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
self.closeButton = QtGui.QPushButton(self.centralwidget)
self.closeButton.setText("Close")
self.closeButton.clicked.connect(self.exit)
def show(self):
QMainWindow.show(self)
myDialog = MyDialog(self)
res = myDialog.exec_()
if res == 0:
self.exit()
else:
print "Continue"
def exit(self):
self.close()
MyDialog.py:
from PyQt4.QtGui import QDialog
from PyQt4 import QtGui
class MyDialog(QDialog):
def __init__(self, parent = None):
QDialog.__init__(self, parent)
self.setWindowTitle("Modal Dialog")
self.resize(200, 50)
self.closeButton = QtGui.QPushButton(self)
self.closeButton.setText("Close")
self.closeButton.move(10, 10)
self.otherButton = QtGui.QPushButton(self)
self.otherButton.setText("Do Nothing")
self.otherButton.move(100, 10)
self.closeButton.clicked.connect(self.reject)
self.otherButton.clicked.connect(self.accept)
如果单击模式对话框上的关闭按钮,应用程序将继续 运行,即使所有 windows 都已关闭。如果单击 "Do Nothing" 按钮,并使用主 window 上的关闭按钮关闭应用程序,则一切正常,应用程序终止。
我真的看不出这两种情况之间的区别,因为每次我只是在主 window 上调用 close
。我可以假设我在处理模态对话框时出错。我什至尝试修改 myDialog.close()
、myDialog.destroy()
以及 quitOnClose
和 deleteOnClose
window 属性,但没有任何积极效果。
感谢任何帮助。
如果单击模态对话框上的关闭按钮,self.exit() 在 app.exec_() 之前被调用,因此应用程序继续 运行。 self.exit() 应在主事件循环为 运行ning 时调用。
以下是该案例的可能解决方案:
from PyQt4.QtGui import QMainWindow
from PyQt4 import QtGui
from pyQt4 import QtCore
from MyDialog import MyDialog
class MyMainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setWindowTitle("Close App Test")
self.centralwidget = QtGui.QWidget(self)
self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
self.closeButton = QtGui.QPushButton(self.centralwidget)
self.closeButton.setText("Close")
self.closeButton.clicked.connect(self.exit)
def login(self):
myDialog = MyDialog(self)
res = myDialog.exec_()
if res == 0:
self.exit()
else:
print "Continue"
def show(self):
QMainWindow.show(self)
QtCore.QTimer.singleShot(0, self.login)
def exit(self):
self.close()
我目前正在使用 Qt4 python 绑定编写应用程序,该绑定要求用户提供登录凭据。在我的应用程序启动时,我会显示一个模式对话框,用户可以在其中输入他们的数据。由于应用程序在用户未登录时无法提供有用的服务,所以我想在用户单击取消按钮时关闭应用程序。
因此,如果对话框 returns 的结果是否定的,我只需调用 QMainWindow
的 close()
方法。通常这会导致应用程序退出,因为不再有可交互的 windows。
但是,如果以前显示过模态对话框,应用程序只会继续 运行,我必须手动将其终止。
下面是一个最小示例的代码
main.py:
import sys
from PyQt4.QtGui import QApplication
from MyMainWindow import MyMainWindow
app = QApplication(sys.argv)
window = MyMainWindow()
window.show()
app.exec_()
print "Terminated"
MyMainWindow.py:
from PyQt4.QtGui import QMainWindow
from PyQt4 import QtGui
from MyDialog import MyDialog
class MyMainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setWindowTitle("Close App Test")
self.centralwidget = QtGui.QWidget(self)
self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
self.closeButton = QtGui.QPushButton(self.centralwidget)
self.closeButton.setText("Close")
self.closeButton.clicked.connect(self.exit)
def show(self):
QMainWindow.show(self)
myDialog = MyDialog(self)
res = myDialog.exec_()
if res == 0:
self.exit()
else:
print "Continue"
def exit(self):
self.close()
MyDialog.py:
from PyQt4.QtGui import QDialog
from PyQt4 import QtGui
class MyDialog(QDialog):
def __init__(self, parent = None):
QDialog.__init__(self, parent)
self.setWindowTitle("Modal Dialog")
self.resize(200, 50)
self.closeButton = QtGui.QPushButton(self)
self.closeButton.setText("Close")
self.closeButton.move(10, 10)
self.otherButton = QtGui.QPushButton(self)
self.otherButton.setText("Do Nothing")
self.otherButton.move(100, 10)
self.closeButton.clicked.connect(self.reject)
self.otherButton.clicked.connect(self.accept)
如果单击模式对话框上的关闭按钮,应用程序将继续 运行,即使所有 windows 都已关闭。如果单击 "Do Nothing" 按钮,并使用主 window 上的关闭按钮关闭应用程序,则一切正常,应用程序终止。
我真的看不出这两种情况之间的区别,因为每次我只是在主 window 上调用 close
。我可以假设我在处理模态对话框时出错。我什至尝试修改 myDialog.close()
、myDialog.destroy()
以及 quitOnClose
和 deleteOnClose
window 属性,但没有任何积极效果。
感谢任何帮助。
self.exit() 在 app.exec_() 之前被调用,因此应用程序继续 运行。 self.exit() 应在主事件循环为 运行ning 时调用。
以下是该案例的可能解决方案:
from PyQt4.QtGui import QMainWindow
from PyQt4 import QtGui
from pyQt4 import QtCore
from MyDialog import MyDialog
class MyMainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setWindowTitle("Close App Test")
self.centralwidget = QtGui.QWidget(self)
self.verticalLayout = QtGui.QVBoxLayout(self.centralwidget)
self.closeButton = QtGui.QPushButton(self.centralwidget)
self.closeButton.setText("Close")
self.closeButton.clicked.connect(self.exit)
def login(self):
myDialog = MyDialog(self)
res = myDialog.exec_()
if res == 0:
self.exit()
else:
print "Continue"
def show(self):
QMainWindow.show(self)
QtCore.QTimer.singleShot(0, self.login)
def exit(self):
self.close()