我使用pyside2来测试对话框,但是下面的程序不会停止,我不知道为什么。 如何停止exce_()函数?
import sys
from PySide2 import QtWidgets, QtGui, QtCore
class MyDialog(QtWidgets.QDialog):
def __init__(self, parent=None):
super(MyDialog, self).__init__(parent)
layout = QtWidgets.QVBoxLayout()
buttons = QtWidgets.QDialogButtonBox(
QtWidgets.QDialogButtonBox.Ok |QtWidgets.QDialogButtonBox.Cancel,
QtCore.Qt.Horizontal, self)
buttons.accepted.connect(self.accept)
buttons.rejected.connect(self.reject)
layout.addWidget(buttons)
self.setLayout(layout)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
win = MyDialog()
result = win.exec_()
print(result)
sys.exit(app.exec_())
答案 0 :(得分:0)
使用win.show()
代替win.exec_()
。
在您的代码中win.exec_()
显示对话框并阻塞主线程,直到您按下确定或取消(或只是关闭对话框)。
来自docs:
进入主事件循环并等待直到调用exit(),然后返回设置为exit()的值(如果通过quit()调用exit()则为0)。
app.exec_()
等待exit()
中的QDialog
,但是在开始等待时,尚无QDialog
,因此没有什么可以触发exit()
方法。