如何在完成此进度条后将其关闭

时间:2023-01-17 15:03:30

I commonly write Python scipts to do conversion tasks for me and whenever I write one that takes a while I use this little progress bar to check on it

我通常编写Python scipts来为我做转换任务,每当我写一个需要一段时间的时候,我会使用这个小进度条来检查它

import sys
import time
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
barra = QtGui.QProgressBar()
barra.show()
barra.setMinimum(0)
barra.setMaximum(10)
for a in range(10):
    time.sleep(1)
    barra.setValue(a)
app.exec_()

I have 2 questions:

我有两个问题:

How do I make it close itself when it reaches 100% (It stays open and if you close the python shell before clicking the X button you crash it.)

当它达到100%时,我如何让它自己关闭(它保持打开状态,如果在点击X按钮之前关闭python shell,你就会崩溃它。)

also, When it loses and regains focus, it stops painting correctly. the process will continue to completion but the progress bar space is all white. How do I handle this?

此外,当它失去并重新获得焦点时,它会停止正确绘画。该过程将继续完成,但进度条空间全是白色。我该如何处理?

1 个解决方案

#1


Well, because you set your Maximum to 10, your progress bar shouldn't reach 100% because

好吧,因为你将你的最大值设置为10,你的进度条不应该达到100%因为

for a in range(10):
  time.sleep(1)
  barra.setValue(a)

will only iterate up to 9.

只会迭代到9。

Progress bars don't close automatically. You will have to call

进度条不会自动关闭。你必须打电话

barra.hide()

after your loop.

循环之后。

As for the paint problem, it's likely because whatever script you ran this script from is in the same thread as the progress bar. So when you switch away and back the paint events are delayed by the actual processing of the parent script. You can either set a timer to periodically call .update() or .repaint() on 'barra' (update() is recommended over repaint()) OR you would want your main processing code to run in a QThread, which is also available in the PyQt code, but that will require some reading on your part :)

至于绘制问题,可能是因为您运行此脚本的任何脚本与进度条位于同一个线程中。因此,当您切换并返回时,绘制事件会因父脚本的实际处理而延迟。你可以设置一个定时器来定期在'barra'上调用.update()或.repaint()(建议在repaint()上使用update())或者你希望你的主处理代码在QThread中运行,这也是在PyQt代码中可用,但这需要你的一些阅读:)

The doc is for Qt, but it applies to PyQt as well:

该文档适用于Qt,但它也适用于PyQt:

https://doc.qt.io/qt-4.8/threads.html

#1


Well, because you set your Maximum to 10, your progress bar shouldn't reach 100% because

好吧,因为你将你的最大值设置为10,你的进度条不应该达到100%因为

for a in range(10):
  time.sleep(1)
  barra.setValue(a)

will only iterate up to 9.

只会迭代到9。

Progress bars don't close automatically. You will have to call

进度条不会自动关闭。你必须打电话

barra.hide()

after your loop.

循环之后。

As for the paint problem, it's likely because whatever script you ran this script from is in the same thread as the progress bar. So when you switch away and back the paint events are delayed by the actual processing of the parent script. You can either set a timer to periodically call .update() or .repaint() on 'barra' (update() is recommended over repaint()) OR you would want your main processing code to run in a QThread, which is also available in the PyQt code, but that will require some reading on your part :)

至于绘制问题,可能是因为您运行此脚本的任何脚本与进度条位于同一个线程中。因此,当您切换并返回时,绘制事件会因父脚本的实际处理而延迟。你可以设置一个定时器来定期在'barra'上调用.update()或.repaint()(建议在repaint()上使用update())或者你希望你的主处理代码在QThread中运行,这也是在PyQt代码中可用,但这需要你的一些阅读:)

The doc is for Qt, but it applies to PyQt as well:

该文档适用于Qt,但它也适用于PyQt:

https://doc.qt.io/qt-4.8/threads.html