完成后如何让进度条关闭

发布于 2024-07-16 14:16:50 字数 497 浏览 2 评论 0原文

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

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_()

我有 2 个问题:

当它达到 100% 时,如何让它自行关闭 (它保持打开状态,如果您在单击 X 按钮之前关闭 python shell,则会使其崩溃。)

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

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

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.)

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的奇迹 2024-07-23 14:16:50

好吧,因为您将“最大值”设置为 10,所以进度条不应达到 100%,因为

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

只会迭代到 9。

进度条不会自动关闭。 您必须

barra.hide()

在循环后调用。

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

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

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

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

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

will only iterate up to 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 :)

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文