1.利用计时器模块QTimer
# 初始化一个定时器
self.timer = QTimer(self)
# 将定时器超时信号与槽函数showTime()连接
self.timer.timeout.connect(self.showTime)
self.timer.start(1000)启动timer
self.timer.stop() 停止timer
#!usr/bin/python
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QListWidget, QGridLayout, QLabel
from PyQt5.QtCore import QTimer, QDateTime
import sys
class WinForm(QWidget):
def __init__(self, parent=None):
super(WinForm, self).__init__(parent)
self.setWindowTitle("QTimer demo")
self.label = QLabel('显示当前时间')
self.startBtn = QPushButton('开始')
self.endBtn = QPushButton('结束')
layout = QGridLayout(self)
# 初始化一个定时器
self.timer = QTimer(self)
# 将定时器超时信号与槽函数showTime()连接
self.timer.timeout.connect(self.showTime)
layout.addWidget(self.label, 0, 0, 1, 2)
layout.addWidget(self.startBtn, 1, 0)
layout.addWidget(self.endBtn, 1, 1)
# 连接按键操作和槽函数
self.startBtn.clicked.connect(self.startTimer)
self.endBtn.clicked.connect(self.endTimer)
self.setLayout(layout)
def showTime(self):
# 获取系统现在的时间
time = QDateTime.currentDateTime()
# 设置系统时间显示格式
timeDisplay = time.toString("yyyy-MM-dd hh:mm:ss dddd")
# 在标签上显示时间
self.label.setText(timeDisplay)
def startTimer(self):
# 设置计时间隔并启动,每隔1000毫秒(1秒)发送一次超时信号,循环进行
self.timer.start(1000)
self.startBtn.setEnabled(False)
self.endBtn.setEnabled(True)
def endTimer(self):
self.timer.stop()
self.startBtn.setEnabled(True)
self.endBtn.setEnabled(False)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = WinForm()
form.show()
sys.exit(app.exec_())
2.使用多线程模块QThread
1.创建类继承QThread,重写run方法,
2.定义sinOut = pyqtSignal(str)run方法中
self.sinOut.emit(str(a))发送信号
3.接收信号
self.thread = MyThread()
# self.thread.setIdentity("")
self.thread.sinOut.connect(self.outText),调用槽函数接收
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Main(QWidget):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
##创建一个线程实例并设置名称、变量、信号槽
# self.thread.setVal(100)
layout = QFormLayout()
self.edita1 = QLineEdit()
self.startbtn = QPushButton()
self.startbtn.setText("开始")
self.stopbtn = QPushButton()
self.stopbtn.setText("停止")
layout.addRow("A数值", self.edita1)
layout.addRow("开始按钮", self.startbtn)
layout.addRow("停止按钮", self.stopbtn)
self.setLayout(layout)
self.startbtn.clicked.connect(self.start)
self.stopbtn.clicked.connect(self.stop)
self.thread = MyThread()
# self.thread.setIdentity("")
self.thread.sinOut.connect(self.outText)
def outText(self, text):
self.edita1.setText(text)
def start(self):
print("开始")
self.thread.setVal(True)
def stop(self):
print("停止")
self.thread.setVal(False)
class MyThread(QThread):
sinOut = pyqtSignal(str)
def __init__(self, parent=None):
super(MyThread, self).__init__(parent)
self.identity = None
self.is_stop = False
def setVal(self, val):
self.is_stop = val
##执行线程的run方法
self.start()
def run(self):
print("run")
print(self.is_stop)
a=1
while self.is_stop:
##发射信号
self.sinOut.emit(str(a))
import time
time.sleep(0.5)
a+=1
print("======")
app = QApplication([])
main = Main()
main.show()
app.exec_()
3.使用事件处理功能
1.
self.timerid = self.startTimer(100)启动timer
2.重写timerEvent进行ui修改
3.self.killTimer(self.timerid)停止timer
#!usr/bin/python
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QWidget, QFormLayout, QLineEdit, QPushButton, QApplication
class Main(QWidget):
def __init__(self, parent=None):
super(Main, self).__init__(parent)
self.a = 1
##创建一个线程实例并设置名称、变量、信号槽
# self.thread.setVal(100)
layout = QFormLayout()
self.edita1 = QLineEdit()
self.startbtn = QPushButton()
self.startbtn.setText("开始")
self.stopbtn = QPushButton()
self.stopbtn.setText("停止")
layout.addRow("A数值", self.edita1)
layout.addRow("开始按钮", self.startbtn)
layout.addRow("停止按钮", self.stopbtn)
self.setLayout(layout)
self.startbtn.clicked.connect(self.start)
self.stopbtn.clicked.connect(self.stop)
def outText(self, text):
self.edita1.setText(text)
def timerEvent(self, a):
print("=========")
self.edita1.setText(str(self.a))
print("+++++++++++++++")
self.a += 1
def start(self):
print("开始")
self.timerid = self.startTimer(100)
self.startbtn.setEnabled(False)
self.stopbtn.setEnabled(True)
def stop(self):
print("停止")
self.killTimer(self.timerid)
self.startbtn.setEnabled(True)
self.stopbtn.setEnabled(False)
if __name__ == '__main__':
app = QApplication([])
main = Main()
main.show()
app.exec_()