PyQt4 GUI 中的标签不会随着 FOR 循环的每个循环而更新 [英] Label in PyQt4 GUI not updating with every loop of FOR loop

查看:29
本文介绍了PyQt4 GUI 中的标签不会随着 FOR 循环的每个循环而更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,我希望使用 GUI 从 Python 程序运行多个命令行函数.我不知道我的问题是否特定于 PyQt4 或者是否与我对 python 代码的使用不当有关.

I'm having a problem, where I wish to run several command line functions from a python program using a GUI. I don't know if my problem is specific to PyQt4 or if it has to do with my bad use of python code.

我希望做的是在我的 GUI 上有一个标签,更改其文本值以通知用户正在执行哪个命令.但是,当我使用 for 循环运行多个命令时,就会出现我的问题.我希望标签在每次循环时更新自身,但是,程序不会在每次循环时更新 GUI 标签,而是仅在 整个 for循环完成,只显示最后执行的命令.

What I wish to do is have a label on my GUI change its text value to inform the user which command is being executed. My problem however, arises when I run several commands using a for loop. I would like the label to update itself with every loop, however, the program is not updating the GUI label with every loop, instead, it only updates itself once the entire for loop is completed, and displays only the last command that was executed.

我的 GUI 环境使用 PyQt4.而且我已经确定标签的文本变量确实随着每个循环而更新,但是,它实际上并没有在 GUI 中直观地显示出来.

I am using PyQt4 for my GUI environment. And I have established that the text variable for the label is indeed being updated with every loop, but, it is not actually showing up visually in the GUI.

有没有办法强制标签自行更新?我在循环中尝试了 update()repaint() 方法,但它们没有任何区别.

Is there a way for me to force the label to update itself? I have tried the update() and repaint() methods within the loop, but they don't make any difference.

我真的很感激任何帮助.谢谢.

I would really appreciate any help. Thank you.

罗尼.

这是我使用的代码:

# -*- coding: utf-8 -*-
import sys, os
from PyQt4 import QtGui, QtCore
Gui = QtGui
Core = QtCore

# ================================================== CREATE WINDOW OBJECT CLASS
class Win(Gui.QWidget):
    def __init__(self, parent = None):
        Gui.QWidget.__init__(self, parent)

        # --------------------------------------------------- SETUP PLAY BUTTON
        self.but1 = Gui.QPushButton("Run Commands",self)
        self.but1.setGeometry(10,10, 200, 100)

        # -------------------------------------------------------- SETUP LABELS
        self.label1 = Gui.QLabel("No Commands running", self)
        self.label1.move(10, 120)

        # ------------------------------------------------------- SETUP ACTIONS
        self.connect(self.but1, Core.SIGNAL("clicked()"), runCommands)


# =======================================================  RUN COMMAND FUNCTION
def runCommands():
    for i in commands:
        win.label1.setText(i)       # Make label display the command being run
        print win.label1.text()     # This shows that the value is actually
                                    # changing with every loop, but its just not
                                    # being reflected in the GUI label
        os.system(i)

# ======================================================================== MAIN

# ------------------------------------------------------  THE TERMINAL COMMANDS
com1 = "espeak 'senntence 1'"
com2 = "espeak 'senntence 2'"
com3 = "espeak 'senntence 3'"
com4 = "espeak 'senntence 4'"
com5 = "espeak 'senntence 5'"
commands = (com1, com2, com3, com4, com5)

# --------------------------------------------------- SETUP THE GUI ENVIRONMENT
app = Gui.QApplication(sys.argv)
win = Win()
win.show()

sys.exit(app.exec_())

推荐答案

标签更新没问题,但是在循环结束之前 GUI 不会重绘.

您可以采取以下措施:

  • 将长时间运行的循环移动到辅助线程,在主线程中绘制 GUI.

  • Move your long-running loop to a secondary thread, drawing the GUI is happening in the main thread.

在循环中调用 app.processEvents().这使 Qt 有机会处理事件并重绘 GUI.

Call app.processEvents() in your loop. This gives Qt the chance to process events and redraw the GUI.

打破你的循环,让它使用 QTimer 超时为 0.

Break up your loop and let it run using a QTimer with a timeout of 0.

使用线程是最好的选择,但比调用 processEvents 涉及的工作要多得多.用计时器来做是老式的方式,不再推荐.(见文档)

Using a thread is the best option, but involves quite a bit more work than just calling processEvents. Doing it with a timer is the old fashioned way and is not recommanded anymore. (see the documentation)

这篇关于PyQt4 GUI 中的标签不会随着 FOR 循环的每个循环而更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆