PyQt进程顺序不正确

2024-04-25 08:37:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着做一个程序,希望过程顺序是:

单击应用按钮, 应用按钮将背景变为绿色; 按钮文本更改为“单击”; 10秒后,“应用”按钮的背景变为蓝色。你知道吗

但现在,在我点击应用按钮之后。按钮文本更改,背景色跳过绿色,直接更改为蓝色。你知道吗

我能得到帮助吗?你知道吗

附加代码:

import time
from PyQt4 import QtGui,QtCore
import sys

class Example(QtGui.QWidget):
    def __init__(self):
        super(Example,self).__init__()
        self.initUI()

    def initUI(self):
        self.Display=QtGui.QLineEdit("Not click",self)
        self.Display.move(30,200)
        self.Button1=QtGui.QPushButton("Apply",self)
        self.Button1.move(30,100)
        self.Button1.clicked.connect(self.DoApply)

        self.setGeometry(300,300,300,300)
        self.setWindowTitle("Review")
        self.show()

    def DoApply(self):
        self.Button1.setStyleSheet("font:13pt; background-color: green")
        self.Display.setText("clicked")
        Dosomething()
        time.sleep(10)
        self.Button1.setStyleSheet("font:13pt; background-color: blue")

def main():
    app=QtGui.QApplication(sys.argv)        
    ex=Example()
    sys.exit(app.exec_())

if __name__=='__main__':
    main()


import sys,time

def Dosomething():
    i=100
    for i in range(10):
        print i, '\n'
        time.sleep(1)

Tags: 文本importselftimemainexampledefdisplay
2条回答

GUI只能在调用事件处理程序之间响应用户事件。因此,如果您有一个需要很长时间才能返回的函数,GUI在函数运行时无法响应,因此无法刷新,等等,它将显示为冻结。最简单的方法是在单独的线程中运行函数,并使用Qt的信号指示任务已完成,从而更新按钮状态(未测试):

class Example(QtGui.QWidget):

    class DoSomething(QtCore.QThread)
         ...
        def run(self):
            ... do stuff that takes a long time ...

    def __init__(self):
        ...
        self.doSomething = None

    def initUI(self):
        ...
        self.Button1.clicked.connect(self.DoApply)
        ...

    def DoApply(self):
        self.Button1.setStyleSheet("font:13pt; background-color: green")
        self.Display.setText("clicked")

          time.sleep(10)   will freeze GUI so perform in separate thread:
        self.doSomething = self.DoSomething()
        def onDone():
            self.doSomething = None
            self.Button1.setStyleSheet("font:13pt; background-color: blue")
        self.doSomething.finished.connect(onDone)
        self.doSomething.start()

当一个信号被发送时,它所连接的插槽通常会同步执行。这意味着它将暂时阻止,任何事件(如绘制小部件背景)在返回之前都不会得到处理。你知道吗

避免这种情况的一种方法是使用timer。这将允许处理挂起的事件,直到计时器发送它的timeout signal,并且调用连接到它的任何插槽。你知道吗

更新

如果您只想在保持ui响应的同时运行循环,只需定期调用qApp.processEvents,如下所示:

    self.Button1.clicked.connect(self.DoApply)
    ...

    def DoApply(self):
        self.Display.setText("clicked")
        # stop user clicking again
        self.Button1.setDisabled(True)
        self.Button1.setStyleSheet("font:13pt; background-color: green")
        # do something...
        for i in range(100):
            # update gui
            QtGui.qApp.processEvents()
            print i
            time.sleep(0.05)
        # allow user to click again
        self.Button1.setDisabled(False)
        self.Button1.setStyleSheet("font:13pt; background-color: blue")

相关问题 更多 >