python 网站建设 拖拽式,教育行业网站建设审批,淘宝网站建设特点,wordpress调用不同头部文件QTimer 前言一、QTimer介绍二、动态时间展示2.1 代码2.2 运行结果 三、定时关闭3.1 介绍他的两种用法1、使用函数或Lambda表达式2、带有定时器类型#xff08;高级#xff09; 3.2 代码3.3 运行结果 总结 前言
好久没学习了。 一、QTimer介绍
pyqt里面的多线程可以有两种实… QTimer 前言一、QTimer介绍二、动态时间展示2.1 代码2.2 运行结果 三、定时关闭3.1 介绍他的两种用法1、使用函数或Lambda表达式2、带有定时器类型高级 3.2 代码3.3 运行结果 总结 前言
好久没学习了。 一、QTimer介绍
pyqt里面的多线程可以有两种实现方式 一、QTimer 二、QThread 多线程同时完成多个任务。
定时器就是每隔一段时间调用一次。
二、动态时间展示
这里介绍基本的调用每个一段时间干一件事情周而复始。
2.1 代码 #Author susocool
#Creattime:2024/6/28
#FileName:48-动态显示当前时间
#Description: 使用QTimer定时器去动态的显示时间
import sys,math
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *class ShowTimerdemo(QWidget):def __init__(self):super().__init__()self.initUI()def initUI(self):self.setWindowTitle(动态显示当前时间)self.resize(500,300)# 设置三个按钮self.label QLabel(显示当前时间)self.startButton QPushButton(开始)self.endButton QPushButton(结束)# 设置布局layout QGridLayout()self.timer QTimer()self.timer.timeout.connect(self.showTime)layout.addWidget(self.label,0,0,1,2)layout.addWidget(self.startButton,1,0) # 第二行第一列layout.addWidget(self.endButton,1,1)self.setLayout(layout)self.startButton.clicked.connect(self.startTimer)self.endButton.clicked.connect(self.endTimer)def showTime(self):# 获取当前时间current_time QDateTime.currentDateTime()current_time_Display current_time.toString(yyyy-MMM-dd hh:mm:ss dddd)self.label.setText(current_time_Display)def startTimer(self):# 设置时间间隔1000msself.timer.start(1000)self.startButton.setEnabled(False)self.endButton.setEnabled(True)def endTimer(self):self.timer.stop()self.startButton.setEnabled(True)self.endButton.setEnabled(False)if __name__ __main__:app QApplication(sys.argv)examlp ShowTimerdemo()examlp.show()sys.exit(app.exec_())
2.2 运行结果 三、定时关闭
QTimer.singleShot 是 PyQt 中的一个在指定的延迟后执行一个函数或方法不需要实例化 QTimer 对象即可使用。
特别适用于那些只需延迟执行一次的操作不需要周期性地调用。
3.1 介绍他的两种用法 1、使用函数或Lambda表达式
singleShot(msec: int, slot: PYQT_SLOT)msec: 指定延迟时间单位为毫秒。 slot: 在延迟后执行的函数或Lambda表达式。 Lambda表达式:一种匿名函数允许您快速定义简单的函数或可调用对象而无需显式地命名它们 2、带有定时器类型高级
singleShot(msec: int, timerType: Qt.TimerType, slot: PYQT_SLOT)msec: 延迟时间单位为毫秒。 timerType: 指定定时器类型通常为 Qt.PreciseTimer 或 Qt.CoarseTimer这是一个高级特性在基本场景中通常不会使用。 slot: 在延迟后执行的函数或Lambda表达式。
3.2 代码 #Author susocool
#Creattime:2024/6/29
#FileName:49-定时关闭
#Description: 执行到一定的时间程序定时关闭
QTimer.singleShot-一定时间只调用一次
import sys,math
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *if __name__ __main__:app QApplication(sys.argv)label QLabel ( font colorred size140bHello World!窗口将在5s后自动关闭b/font )# 设置窗口样式# Qt.SplashScreen 表示窗口是一个启动画面# Qt.FramelessWindowHint 表示窗口无边框label.setWindowFlags(Qt.SplashScreen|Qt.FramelessWindowHint)QTimer.singleShot(5000,app.quit) # 使用 QTimer 的 SingleShot 方法在5秒后自动关闭应用程序label.show()sys.exit(app.exec_())3.3 运行结果 5s之后会消失这个没办法展示出来。只能用我贫瘠的语言描述 总结
这篇文章依旧没有总结