网络编程
位置:首页>> 网络编程>> Python编程>> python GUI库图形界面开发之PyQt5时间控件QTimer详细使用方法与实例

python GUI库图形界面开发之PyQt5时间控件QTimer详细使用方法与实例

作者:jia666666  发布时间:2021-01-04 21:56:00 

标签:python,PyQt5,时间

QTimer控件介绍

如果在应用程序中周期性地进行某项操作,比如周期性的检测主机的cpu值,则需要用到QTimer定时器,QTimer类提供了重复和单次的定时器,要使用定时器,需要先创建一个QTimer实例,将其Timeout信号连接到槽函数,并调用start(),然后,定时器,会以恒定的间隔发出timeout信号

当窗口的控件收到Timeout信号后,他就会停止这个定时器,这是在图形用户界面中实现复杂工作的一个典型用法,随着技术的进步,多线程在越来越多的平台上被使用,QTimer对象会被替代掉

QTimer类中的常用方法

方法描述
start(milliseconds)启动或重新启动定时器,时间间隔为毫秒,如果定时器已经运行,他将停止并重新启动,如果singleSlot信号为真,定时器仅被激活一次
Stop()停止定时器

QTimer类中常用的信号

信号描述
singleShot在给定的时间间隔后调用一个槽函数时发射此信号
timeout当定时器超时时发射此信号

QTimer的使用实例


import sys
from PyQt5.QtWidgets import QWidget,QPushButton,QApplication,QListWidget,QGridLayout,QLabel
from PyQt5.QtCore import QTimer,QDateTime

class WinForm(QWidget):
 def __init__(self,parent=None):
   super(WinForm, self).__init__(parent)
   #设置标题
   self.setWindowTitle('QTimer demo')

#实例化一些控件
   self.listFile=QListWidget()
   self.lable=QLabel('显示当前时间')
   self.startBtn=QPushButton('开始')
   self.endBtn=QPushButton('结束')

#栅格布局
   layout=QGridLayout()

#初始化一个定时器
   self.timer=QTimer()
   #定时器结束,触发showTime方法
   self.timer.timeout.connect(self.showTime)

#添加控件到栅格指定位置
   layout.addWidget(self.lable,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.lable.setText(timeDisplay)
 def startTimer(self):
   #设置时间间隔并启动定时器
   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_())

运行效果图

python GUI库图形界面开发之PyQt5时间控件QTimer详细使用方法与实例

代码分析

首先初始化一个定时器,把定时器的timeout信号与showTime()槽函数连接起来


self.timer=QTimer(self)
self.timer.timeout.connect(self.showTime)

使用连接的槽函数显示当前时间,并在标签上显示系统当前的时间


def showTime(self):
   #获取系统当前时间
   time=QDateTime.currentDateTime()
   #设置系统时间的显示格式
   timeDisplay=time.toString('yyyy-MM-dd hh:mm:ss dddd')
   #在标签上显示时间
   self.lable.setText(timeDisplay)

单击开始按钮,启动定时器,并使按钮失效


   #设置时间间隔并启动定时器
   self.timer.start(1000)
   #设置开始按钮不可点击,结束按钮可点击
   self.startBtn.setEnabled(False)
   self.endBtn.setEnabled(True)

单击结束按钮,停止定时器,并使按钮失效


   #停止定时器
   self.timer.stop()
   #结束按钮不可点击,开始按钮可以点击
   self.startBtn.setEnabled(True)
   self.endBtn.setEnabled(False)

QTimer小应用实例


import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

if __name__ == '__main__':
 app=QApplication(sys.argv)
 #设置标签以及文本内容
 label=QLabel('<font color=red size=128><b>Hello PyQt,窗口会在10秒后消失!</b></font>')
 #设置无边框窗口
 label.setWindowFlags(Qt.SplashScreen|Qt.FramelessWindowHint)

label.show()

#设置10秒后自动退出
 QTimer.singleShot(10000,app.quit)

sys.exit(app.exec_())

运行效果如图

python GUI库图形界面开发之PyQt5时间控件QTimer详细使用方法与实例

代码分析

弹出的窗口会在10秒后消失,模仿程序的启动界面,将弹出的窗口设置为无边框


#设置无边框窗口
label.setWindowFlags(Qt.SplashScreen|Qt.FramelessWindowHint)

使用QTimer设置时间间隔,10秒后退出程序


#设置10秒后自动退出
QTimer.singleShot(10000,app.quit)

本文详细介绍了PyQt5时间控件QTimer详细使用方法与实例,更多关于PyQt5时间控件QTimer方面的知识请查看下面的相关链接

来源:https://blog.csdn.net/jia666666/article/details/81672344

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com