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

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

问题描述

我有一个问题,我希望使用GUI从python程序运行几个命令行函数。我不知道如果我的问题是特定于 PyQt4 ,或者如果它与我的糟糕的Python代码使用。

我想要做的是在我的GUI上有一个标签,改变它的文本值来通知用户哪个命令正在执行。然而,我的问题出现在我用 for 循环运行几个命令时。我希望标签能够在每个循环中进行更新,但是,程序并没有在每个循环中更新GUI标签,而是仅在整个 循环完成,并只显示最后执行的命令。

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

有没有办法强制标签更新?我已经尝试了循环内的 update() repaint()方法,但是它们没有任何区别。

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

Ronny。

下面是我使用的代码:

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

#========================== ======================== CREATE WINDOW OBJECT CLASS
class Win(Gui.QWidget):
def __init __(self,parent =无):
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运行,自我)
self.label1.move(10,120)

#---------------------- --------------------------------- SETUP ACTIONS
self.connect(self.but1,Core.SIGNAL ( 点击()), runCommands)


#=================================== ==================== RUN COMMAND FUNCTION
def runCommands():
for i in命令:
win.label1.setText (i)#使标签显示正在运行的命令
print win.label1.text()#这显示了值实际上是
#随每个循环而改变,但是它不是
#体现在GUI标签
os.system(i)

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

#------------------------------------------- -----------终端命令
com1 =espeaksenntence 1'
com2 =espeak'senntence 2'
com3 =espeaksenntence 3''
com4 ='espeak'senntence 4''
com5 =espeak'senntence 5'
commands =(com1,com2,com3,com4,com5)

#--------------------------------------------- --- ---设置GUI环境
app = Gui.QApplication(sys.argv)
win = Win()
win.show()

sys.exit (app.exec_())


解决方案

所有的权利,但是在你的循环结束之前,GUI不会被重新绘制。 下面是你可以做的事情:

>


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


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

  • 分解你的循环并让它用 QTimer ,超时时间为0。




    使用线程是最好的选择,但涉及比调用 processEvents 更多的工作。做一个计时器是老式的方式,不再推荐。 (请参阅文档)


    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.

    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.

    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.

    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.

    Ronny.

    Here is the code I am using:

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

    解决方案

    The label gets updated all right, but the GUI isn't redrawn before the end of your loop.

    Here's what you can do about it:

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

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

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

    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天全站免登陆