有没有一种简单的方法可以让线程在后台运行而不中断GUI主循环?

时间:2019-10-13 21:16:39

标签: python multithreading pyqt4 python-2.x

Python 2-允许线程在后台运行而不中断GUI主循环

我正在开发Qt生成的python接口,我想使用在后台运行的线程每4秒更新一次服务器连接状态标签。没有GUI冻结,我似乎无法运行线程。

from PyQt4 import QtCore, QtGui
import sys, threading, time

import generated_layout as gl
import query_interface

stop_threads = False

def thread_check_connnection(ui):
    while not stop_threads:
        if(query_interface.connection_available()):
            ui.mw_connection_label.setText("<font color='green'>Connected</font>")
        else:
            ui.mw_connection_label.setText("<font color='red'>Failed to connect</font>")
        time.sleep(4)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    MainWindow = gl.QtGui.QMainWindow()

    ui = gl.Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()

    t1 = threading.Thread(target = thread_check_connnection, args=(ui, ))
    t1.start()

    app.exec_()
    stop_threads = True
    print "ended"
    sys.exit()

0 个答案:

没有答案