pyQt5实时刷新界面的示例

yipeiwu_com6年前Python基础

如下所示:

from PyQt5.QtCore import QThread , pyqtSignal, QDateTime , QObject
from PyQt5.QtWidgets import QApplication, QDialog, QLineEdit
import time
import sys

class BackendThread(QObject):
  # 通过类成员对象定义信号
  update_date = pyqtSignal(str)
  
  # 处理业务逻辑
  def run(self):
    while True:
      data = QDateTime.currentDateTime()
      currTime = data.toString("yyyy-MM-dd hh:mm:ss")
      self.update_date.emit( str(currTime) )
      time.sleep(1)

class Window(QDialog):
  def __init__(self):
    QDialog.__init__(self)
    self.setWindowTitle('PyQt 5界面实时更新例子')
    self.resize(400, 100)
    self.input = QLineEdit(self)
    self.input.resize(400, 100)
    self.initUI()

  def initUI(self):
    # 创建线程
    self.backend = BackendThread()
    # 连接信号
    self.backend.update_date.connect(self.handleDisplay)
    self.thread = QThread()
    self.backend.moveToThread(self.thread)
    # 开始线程
    self.thread.started.connect(self.backend.run)
    self.thread.start()

  # 将当前时间输出到文本框
  def handleDisplay(self, data):
    self.input.setText(data)

if __name__ == '__main__':
  app = QApplication(sys.argv)
  win = Window()
  win.show() 
  sys.exit(app.exec_())

以上这篇pyQt5实时刷新界面的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

详解 Python 与文件对象共事的实例

详解 Python 与文件对象共事的实例 Python 有一个内置函数,open,用来打开在磁盘上的文件。open 返回一个文件对象,它拥有一些方法和属性,可以得到被打开文件的信息,以及...

在双python下设置python3为默认的方法

在双python下设置python3为默认的方法

如何在双python下设置python3为默认 在C:\Program下举例 第一步安装好python2和python3后设置好环境变量 第二步去掉python2根目录下的python....

21行Python代码实现拼写检查器

引入 大家在使用谷歌或者百度搜索时,输入搜索内容时,谷歌总是能提供非常好的拼写检查,比如你输入 speling,谷歌会马上返回 spelling。 下面是用21行python代码实现的一...

Numpy之reshape()使用详解

Numpy之reshape()使用详解

如下所示: Numpy中reshape的使用方法为:numpy.reshape(a, newshape, order='C') 参数详解: 1.a: type:array_like(伪数...

Python中类似于jquery的pyquery库用法分析

本文实例讲述了Python中类似于jquery的pyquery库用法。分享给大家供大家参考,具体如下: pyquery:一个类似于jquery的Python库 pyquery可以使你在x...