如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
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实时刷新界面的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_34696203/article/details/86569791