【发布时间】:2017-03-14 20:44:17
【问题描述】:
我正在尝试创建一个弹出的对话框窗口,运行一个函数,然后在函数完成后自动关闭。在下面的代码中,该函数在弹出对话框之前运行,我无法自动关闭,否则会弹出对话框窗口并且对单击“x”按钮没有响应。
如何创建弹出窗口,在弹出窗口可见后运行函数,然后在函数运行完成后关闭弹出窗口。
# from PyQt4 import QtGui
# QtWidgets = QtGui
from PyQt5 import QtWidgets, QtCore
import sys, time
app = QtWidgets.QApplication(sys.argv)
def print_every_3_seconds():
print(0)
for i in range(1, 4):
time.sleep(1)
print(i)
class RunFunctionDialog(QtWidgets.QDialog):
def __init__(self, function, parent=None):
super(RunFunctionDialog, self).__init__(parent)
self.layout = QtWidgets.QVBoxLayout(self)
self.textBrowser = QtWidgets.QTextBrowser()
self.textBrowser.setText("Wait 3 seconds")
self.layout.addWidget(self.textBrowser)
self.function = function
def showEvent(self, QShowEvent):
self.function()
# self.close() # dialog freezes in an unresponsive state
def show_dialog():
dialog = RunFunctionDialog(print_every_3_seconds)
dialog.exec_()
widget = QtWidgets.QWidget(None)
button = QtWidgets.QPushButton("Show Dialog", widget)
button.clicked.connect(show_dialog)
widget.show()
app.exec_()
【问题讨论】:
-
你可以看看QProcessDialog