我有一个简单的程序,我想有一个模式,无阻塞的进度窗口(使用QProgressDialog)远程更新。SIZE
只控制QProgressDialog的最大值。但是,如果我将其值设置为小于等于4,则在整个操作期间,窗口将如下所示:
换句话说,窗口是完全白色的,不显示文本或进度条。如果我将SIZE
的值设置为5或更多,则显示工作正常,但仅在2-3次第一次迭代之后:
后来呢
import sys, time
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
SIZE = 5
def doGenerate(setValue):
for x2 in range(SIZE):
time.sleep(1)
setValue(x2 + 1)
print('Done')
class MainMenu(QMainWindow):
def __init__(self):
super().__init__()
self.genAudioButton = QPushButton('Generate', self)
self.genAudioButton.clicked.connect(self.generate)
self.setCentralWidget(self.genAudioButton)
self.show()
def generate(self):
try:
progress = QProgressDialog('Work in progress', '', 0, SIZE, self)
progress.setWindowTitle("Generating files...")
progress.setWindowModality(Qt.WindowModal)
progress.show()
progress.setValue(0)
doGenerate(progress.setValue)
except Exception as e:
errBox = QMessageBox()
errBox.setWindowTitle('Error')
errBox.setText('Error: ' + str(e))
errBox.addButton(QMessageBox.Ok)
errBox.exec()
return
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MainMenu()
ret = app.exec_()
sys.exit(ret)
是什么造成的?我该怎么解决它?在
另外,是否有一种方法可以完全删除“取消”按钮,而不是使用一个空按钮来仍然取消操作?PyQT4文档(我使用PyQT5)表示空字符串应该达到这个结果,QT5的C++ DOCs表示相同,但显然在这里不起作用。我没有找到PyQt5的独立文档。在
几乎就是导致我来这里的问题。空白的,白色的对话框,然后突然正确显示,但好像已经发生了2到3次迭代。在
对我来说这个解决方案毫无意义。。。在
几乎就像第一个StValk给出了空白对话框,接下来的两个执行前两次迭代,所以第一个真正的迭代有一个正确显示对话框来更新…在
GUI通过
app.exec_()
实现了一个主循环,该循环用于执行诸如检查事件、信号、调用某些函数等任务。因此,如果我们中断循环,我们可以得到与您观察到的类似的意外行为。在您的例子中,sleep()
是一个不应该使用的阻塞函数,Qt提供了它的替代方法,其中之一是使用QEventLoop
和QTimer
:如果不想显示“取消”按钮,则必须传递“无”:
^{pr2}$如果您想使用}。我们将使用
gTTS
,您必须通过线程来实现它,Qt提供了几种实现它的方法,在本例中,我将使用QThreadPool
和{QMetaObject.invokeMethod
来更新GUI的值,因为Qt禁止从非主线程的另一个线程更新GUI。在这对使用Quamash/asyncio进行异步应用程序的任何人都可能有用。在
它以@eyllanesc为例,在执行器中分派一个CPU绑定的任务,并删除对GTT的依赖。在
同样出于我的目的,我不知道CPU绑定需要多长时间,所以我将progress对话框的最小值和最大值都设置为零。这有一个很好的效果,就是在任务完成之前对进度条设置动画。但是,在执行此操作时必须手动调用
cancel()
方法,因为进度对话框无法知道它何时完成。这是在附加到未来的回调中完成的。在相关问题 更多 >
编程相关推荐