Python 使用 Qt,事件循环 [英] Python working with Qt, event loop

查看:62
本文介绍了Python 使用 Qt,事件循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了事件循环问题.要理解,我想更新 Label/textBrowser/lineEdit在某些循环期间未按下停止按钮时的文本.

I have a problem with event loop. To understand, i want to update Label/textBrowser/lineEdit text during some loop while Stop button isn't pushed.

def __init__(self):
    QtGui.QMainWindow.__init__(self)
    self.ui = Ui_MainWindow()
    self.ui.setupUi(self)
    self.ui.btnStart.clicked.connect(self.btnStart_Clicked)
    self.ui.btnStop.clicked.connect(self.btnStop_Clicked)
    self.startTime = dt.datetime(2013, 1, 1, 0, 0, 0)
    self.nowTime = dt.datetime(2013, 1, 1, 0, 0, 0)
    self.ui.lineStart.setText(self.startTime.strftime("%Y-%m-%d %H:%M"))
    self.ui.lineNow.setText(self.nowTime.strftime("%Y-%m-%d %H:%M"))
    self.ui.textBrowser.setText(self.nowTime.strftime("%Y-%m-%d %H:%M"))



def btnStart_Clicked(self):
    print(12)
    while (not self.btnStop_Clicked()):
        print(23)
        self.nowTime += dt.timedelta(minutes = 25)
        self.ui.textBrowser.setText(self.nowTime.strftime("%Y-%m-%d %H:%M"))
        time.sleep(2)

推荐答案

Qt 在空闲时更新它的 Gui.为了克服这个问题,您应该使用线程或产生更新功能,例如通过使用计时器进行更新.下面是我用来每 100 毫秒重新安排一次更新的一些代码.用例与您的类似.

Qt Updates its Gui when it's Idle. To overcome this, you should use threading or yield your update function e.g. by using a timer to update instead. Below is some code I use to reschedule an update each 100ms. The use case is similar to yours.

    self.timer = QtCore.QTimer(self.wid)
    self.timer.timeout.connect(self.UpdateTab)
    self.timer.start(100)  

这篇关于Python 使用 Qt,事件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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