PyQt5 - 双QTimer实现并行输出

article/2025/9/13 14:43:56
  • QTime的使用

  • 双Qtime的实现原理



 

一:QTime的使用



# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'D:\Qt\QT-Projects\UI项目\时间实时更新.ui'
#
# Created by: PyQt5 UI code generator 5.12.2
#
# WARNING! All changes made in this file will be lost!from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QTimer,QDateTime
from PyQt5.QtWidgets import QApplication,QDialog
import sysclass Ui_Dialog(object):def setupUi(self, Dialog):Dialog.setObjectName("Dialog")Dialog.resize(546, 272)Dialog.setMinimumSize(QtCore.QSize(546, 272))Dialog.setMaximumSize(QtCore.QSize(546, 272))Dialog.setStyleSheet("")self.label = QtWidgets.QLabel(Dialog)self.label.setGeometry(QtCore.QRect(90, 70, 351, 51))font = QtGui.QFont()font.setPointSize(13)self.label.setFont(font)self.label.setStyleSheet("background-color: rgb(85, 255, 127);")self.label.setText("")self.label.setObjectName("label")self.pushButton_start = QtWidgets.QPushButton(Dialog)self.pushButton_start.setGeometry(QtCore.QRect(90, 160, 141, 51))font = QtGui.QFont()font.setPointSize(12)self.pushButton_start.setFont(font)self.pushButton_start.setObjectName("pushButton_start")self.pushButton_stop = QtWidgets.QPushButton(Dialog)self.pushButton_stop.setGeometry(QtCore.QRect(270, 160, 171, 51))font = QtGui.QFont()font.setPointSize(12)self.pushButton_stop.setFont(font)self.pushButton_stop.setObjectName("pushButton_stop")self.retranslateUi(Dialog)QtCore.QMetaObject.connectSlotsByName(Dialog)def retranslateUi(self, Dialog):_translate = QtCore.QCoreApplication.translateDialog.setWindowTitle(_translate("Dialog", "计时器"))self.pushButton_start.setText(_translate("Dialog", "开始"))self.pushButton_stop.setText(_translate("Dialog", "停止"))self.timer = QTimer(Dialog)self.timer.timeout.connect(self.show_time)self.pushButton_stop.setEnabled(False)self.pushButton_start.setEnabled(True)self.pushButton_start.clicked.connect(self.start_timer)self.pushButton_stop.clicked.connect(self.stop_timer)def start_timer(self):self.timer.start(1000)self.pushButton_stop.setEnabled(True)self.pushButton_start.setEnabled(False)def stop_timer(self):self.timer.stop()self.pushButton_start.setEnabled(True)self.pushButton_stop.setEnabled(False)def show_time(self):time = QDateTime.currentDateTime()time_display = time.toString("yyyy-MM-dd hh:mm:ss dddd")self.label.setText("  "+time_display)if __name__ == "__main__":app = QApplication(sys.argv)window = QDialog()ui = Ui_Dialog()ui.setupUi(window)qss_style = """QLabel{border-radius:15px;}QPushButton{border-radius:15px;background-color:red;}QPushButton:hover{background-color:blue;}"""window.setStyleSheet(qss_style)window.show()sys.exit(app.exec_())

 

二:双Qtime的实现原理

测试用例:

# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'D:\Qt\QT-Projects\UI项目\时间实时更新.ui'
#
# Created by: PyQt5 UI code generator 5.12.2
#
# WARNING! All changes made in this file will be lost!from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QTimer,QDateTime
from PyQt5.QtWidgets import QApplication,QDialog
import sysclass Ui_Dialog(object):def setupUi(self, Dialog):Dialog.setObjectName("Dialog")Dialog.resize(546, 272)Dialog.setMinimumSize(QtCore.QSize(546, 272))Dialog.setMaximumSize(QtCore.QSize(546, 272))Dialog.setStyleSheet("")self.label = QtWidgets.QLabel(Dialog)self.label.setGeometry(QtCore.QRect(90, 70, 351, 51))font = QtGui.QFont()font.setPointSize(13)self.label.setFont(font)self.label.setStyleSheet("background-color: rgb(85, 255, 127);")self.label.setText("")self.label.setObjectName("label")self.pushButton_start = QtWidgets.QPushButton(Dialog)self.pushButton_start.setGeometry(QtCore.QRect(90, 160, 141, 51))font = QtGui.QFont()font.setPointSize(12)self.pushButton_start.setFont(font)self.pushButton_start.setObjectName("pushButton_start")self.pushButton_stop = QtWidgets.QPushButton(Dialog)self.pushButton_stop.setGeometry(QtCore.QRect(270, 160, 171, 51))font = QtGui.QFont()font.setPointSize(12)self.pushButton_stop.setFont(font)self.pushButton_stop.setObjectName("pushButton_stop")self.retranslateUi(Dialog)QtCore.QMetaObject.connectSlotsByName(Dialog)def retranslateUi(self, Dialog):_translate = QtCore.QCoreApplication.translateDialog.setWindowTitle(_translate("Dialog", "计时器"))self.pushButton_start.setText(_translate("Dialog", "开始"))self.pushButton_stop.setText(_translate("Dialog", "停止"))self.timer = QTimer(Dialog)self.timer_2 = QTimer(Dialog)self.timer.timeout.connect(self.show_timer_1)self.timer_2.timeout.connect(self.show_time_2)self.pushButton_stop.setEnabled(False)self.pushButton_start.setEnabled(True)self.pushButton_start.clicked.connect(self.start_timer)self.pushButton_stop.clicked.connect(self.stop_timer)def start_timer(self):self.timer.start(500)print("计时器1打开操作 - 已经执行!")self.timer_2.start(500)print("计时器2打开操作 - 已经执行!")self.pushButton_stop.setEnabled(True)self.pushButton_start.setEnabled(False)def stop_timer(self):self.timer.stop()#self.timer_2.stop()print("计时器1关闭操作 - 已经执行!")self.pushButton_start.setEnabled(True)self.pushButton_stop.setEnabled(False)def show_timer_1(self):print(">>>计时器1的输出!")def show_time_2(self):print(">>>计时器2的输出!")#pass# time = QDateTime.currentDateTime()# time_display = time.toString("yyyy-MM-dd hh:mm:ss dddd")# self.label.setText("  "+time_display)if __name__ == "__main__":app = QApplication(sys.argv)window = QDialog()ui = Ui_Dialog()ui.setupUi(window)qss_style = """QLabel{border-radius:15px;}QPushButton{border-radius:15px;background-color:red;}QPushButton:hover{background-color:blue;}"""window.setStyleSheet(qss_style)window.show()sys.exit(app.exec_())

测试结果:

 

 


http://chatgpt.dhexx.cn/article/t3kgLORE.shtml

相关文章

QTimer 定时器

QTimer类为我们提供了一个即可重复触发又可单次触发的定时器。它是一个高层次的应用程序接口。要使用它,只需创建一个QTimer类对象,将它的timeout()信号连接到适当的函数上,然后调用其start()函…

QTimer使用

QTimer工作流程 程序示例 test_timee.h //继承QObject,使用信号/槽 class TestTimer: public QObject {Q_OBJECT public:TestTimer(QObject *parent nullptr);public slots:void start();void stop();void do1();private://定义timer对象QTimer m_timer; }; tes…

定时器QTimer

学习PyQt推荐大家看这本书:https://weread.qq.com/web/reader/6393267071ccfa97639f573 链接:https://pan.baidu.com/s/1ZuHxNvEYtUqzSWqytN7viw 提取码:qku8 import sys from PyQt5.QtWidgets import QApplication,QWidget from PyQt5 i…

Qt QTimer定时器

1.QTimer简介 QTimer 主要的属性是 interval,是定时中断的周期,单位毫秒。QTimer 主要的信号是 timeout(),在定时中断时发射此信号,要想在定时中断里做出响应,这就需要编写 timeout() 信号的槽函数。 2.常用API //设…

Qt定时器QTimer使用教程与代码演示

Qt提供了定时器类QTimer, 在使用时需要包含头文件 #include <QTimer>QTimer类方法介绍: void start(int msec); 开启定时器&#xff0c;定时间隔的msec毫秒void stop(); 结束定时 QTimer信号&#xff1a; void timeout(QPrivateSignal); 在链接定时器时&#xff0c;需…

【Qt】QTimer的简单使用

定义定时器对象&#xff1a;QTimer *myTimer; 动态分部内存空间&#xff1a;myTimer new QTimer(this); 启动定时器&#xff1a;myTimer->start(100); 定时器超时事件&#xff1a;QTimer::timeout() 停止定时器&#xff1a;myTimer->stop(); 等等&#xff1b; 程序实现功…

QT之QTimer详解以及结合多线程中开启定时器的示例

一 QTimer详解 QTimer类提供了重复和单次触发信号的定时器。 a.void timeout ()定时器超时后&#xff0c;这个信号被发射。 b.void start()开启定时器,它的重载函数void start(int msec),启动或重新启动一个超时时间间隔为毫秒的定时器,如果定时器正在运行&#xff0c;它将被停…

Qt 之 QTimer

作者&#xff1a; 一去、二三里 个人微信号&#xff1a; iwaleon 微信公众号&#xff1a; 高效程序员 QTimer类提供了重复和单次触发信号的定时器。 QTimer类为定时器提供了一个高级别的编程接口。很容易使用&#xff1a;首先&#xff0c;创建一个QTimer&#xff0c;连接timeo…

DEV、SIT、UAT、PET、SIM、PRD、PROD缩写介绍

按开发、测试、上线的时间线排序&#xff1a; DEV Development 研发环境 SIT System Integrate Test 系统集成测试环境&#xff08;内测&#xff09; UAT User Acceptance Test 用户验收测试环境 PET Performance Evaluation Test 性能评估测试环境&#xff08;压测&#xff0…

什么是SIT, UAT测试

2019独角兽企业重金招聘Python工程师标准>>> SIT测试 SIT(System Integration Testing)系统集成测试&#xff0c;也叫做集成测试&#xff0c;是软件测试的一个术语&#xff0c;在其中单独的软件模块被合并和作为一个组测试。它在单元测试以后和在系统测试之前。集成…

UAT测试和SIT测试的区别

区别如下&#xff1a; 1、UAT:终端用户集成测试&#xff0c;主要是要求用户参与进测试流程&#xff0c;并得到用户对软件的认可&#xff0c;鼓励用户自己进行测试设计和进行破坏性测试&#xff0c;充分暴露系统的设计和功能问题&#xff0c;显然&#xff0c;用户的认可和破坏性…

UT-FT-ST测试

单元测试(UT)、功能测试(FT)&#xff1a; 目的&#xff1a;1、尽量避免写的代码测试人员频繁的来找你其他地方又出问题了&#xff1b;2、提供的接口不可用&#xff1b;3、一个bug修复了引入了其他的bug或者其他用例变红了&#xff1b; 理解&#xff1a;在实现函数功能的时候编…

unittest教程__测试报告(6)

用例执行完成后&#xff0c;执行结果默认是输出在屏幕上&#xff0c;其实我们可以把结果输出到一个文件中&#xff0c;形成测试报告。 unittest自带的测试报告是文本形式的&#xff0c;如下代码&#xff1a; import unittestif __name__ __main__:# 识别指定目录下所有以tes…

Pytest 分组测试

有时候需要针对不同的测试环境跑不同的测试用例&#xff0c;如&#xff1a;冒烟测试、sit、uat、prd&#xff0c;所以给自动化测试用例做标记分组是很有必要的&#xff0c;pytest.mark 可以轻松实现这个功能。首先需要注册自定义标记。 通过使用pytest.mark帮助程序&#xff0…

冒烟测试回归测试UATSIT

在软件研发中&#xff0c;冒烟测试其实是微软首先提出来的一个概念&#xff0c;和微软一直提倡的每日build&#xff08;构建版本&#xff09;有很密切的联系。具体说&#xff0c;冒烟测试就是在每日build&#xff08;构建版本&#xff09;建立后&#xff0c;对系统的基本功能进…

使用GIT提交代码流程

安装步骤&#xff1a;双击Git-2.27.0-64-bit.exe安装 双击TortoiseGit-2.10.0.2-64bit.msi安装 代码提交一般有五个步骤: 1.查看目前代码的修改状态 2.查看代码修改内容 3.暂存需要提交的文件 4.提交已暂存的文件 5.同步到服务器 登陆复制仓库的地址然后复制到下图的Directory…

Git提交代码的流程

企业级项目git远程仓库一般是由master/test/dev三个分支构成&#xff0c;开发人员是在dev上做开发&#xff0c;当需要发布测试环境或者生产环境时管理员会把开发人员提交的代码合并到test或者master上。 开发人员git操作步骤&#xff1a; 1.git clone 把远程dev上的代码克隆到本…

傻瓜式Git提交代码流程【写给初学者】

前言 Git是程序员工作中最最常用的分布式版本控制系统&#xff0c;为的就是解决那种多人协作、多次修改的问题&#xff0c;虽说Git可以应用于各行各业&#xff0c;但是实际上还是程序员用的最多了&#xff0c;应该说是程序员的必备技能之一了&#xff0c;如果你还不会Git&…

vs2019中git提交代码的步骤

目录 前言 一、在vs2019的git界面提交代码 二、使用git命令行提交代码 总结 前言 在vs2019中使用git提交代码的步骤和一些常见问题。 一、在vs2019的git界面提交代码 1.切换到需要提交代码的分支上&#xff0c;首先更新分支。 vs2019工具栏选择Git&#xff0c;点击更新。 2.…

IDEA使用git提交代码

1.将鼠标移动到需要提交的代码文件上&#xff1b;2.右键文件夹&#xff0c;找到Git选项&#xff1b;3.进入选项中的Repository&#xff1b;4.然后点击push就可以将暂存的代码提交到 Git 服务器上。