PyQt5 - QWidgets部件进阶教程之数字时钟
来源:互联网 发布:软件系统项目总结报告 编辑:程序博客网 时间:2024/06/06 03:59
- 废话
- 定义数字时钟类
- 实现数字时钟类
- 整体代码
- 最终效果
- 相关类
- 相关方法
废话
该案例演示QTimer如何在规定时间更新部件。
定义数字时钟类
数字时钟类提供一个显示小时和分钟的部件,我们子类化QLCDNumber,并实现一个showTime()槽来更新时钟显示。
from PyQt5.QtWidgets import *from PyQt5.QtCore import *import sysclass DigitalClock(QLCDNumber): def __init__(self): super(DigitalClock, self).__init__()
实现数字时钟类
class DigitalClock(QLCDNumber): def __init__(self): super(DigitalClock, self).__init__() self.setWindowTitle('Digital Clock') self.resize(150, 60) self.setSegmentStyle() self.timer = QTimer(self) self.timer.timeout.connect(self.showTime()) self.timer.start(1000)
- 在构造器内,我们先改变LCD数字外观, QLCDNumber默认为Filled风格,即黑色凸起的分段数字。
- 用start(1000)设置秒计时器来保持与当前时间同步
- 将timeout()信号与自定义showTime()槽连接,依次来显示每秒更新。
def showTime(self): time = QTime.currentTime() text = time.toString('hh:mm') if time.second() % 2 == 0: text = text[:2] + ' ' + text[3:] self.display(text)
- 当时钟更新时showTime()被调用
- 用’hh:mm’格式将当前时间转化成字符串
- 当QTime.currentTime().second()为偶数时,字符串中的冒号替换为空格,这样会使冒号在每秒时显示和隐藏
- 最后我们调用QLCDNumber.display()来更新部件
整体代码
from PyQt5.QtWidgets import *from PyQt5.QtCore import *import sysclass DigitalClock(QLCDNumber): def __init__(self): super(DigitalClock, self).__init__() self.setWindowTitle('Digital Clock') self.resize(150, 60) self.setSegmentStyle(QLCDNumber.Filled) self.timer = QTimer(self) self.timer.timeout.connect(self.showTime) self.timer.start(1000) def showTime(self): time = QTime.currentTime() text = time.toString('hh:mm') if time.second() % 2 == 0: text = text[:2] + ' ' + text[3:] self.display(text)app = QApplication(sys.argv)dc = DigitalClock()dc.show()app.exec_()
最终效果
相关类
- QLCDNumber
- QTimer
- QTime
相关方法
- QLCDNumber
- setSegmentStyle
- display
- QTimer
- timeout
- start
- QTime
- currentTime()
- second()
- toString()
0 0
- PyQt5 - QWidgets部件进阶教程之数字时钟
- PyQt5 - QWidgets部件进阶教程之模拟时钟
- PyQt5 - QWidgets部件进阶教程之塑形时钟
- PyQt5 - QWidgets部件进阶教程之计算器
- PyQt5 - QWidgets部件进阶教程
- PyQt5 - QWidgets部件进阶教程之日历窗口部件
- PyQt5 - QWidgets部件进阶教程之字符映射表
- PyQt5 - QWidgets部件进阶教程之分组框
- PyQt5 - QWidgets部件进阶教程之行编辑
- PyQt5 - QWidgets部件入门教程
- 我的Android进阶之旅------>Android Widget 桌面数字时钟(DigtalClockWidget)实例
- PyQt5:QCalendarWidget日历部件(27)
- 数字部件
- 《Windows程序设计》之数字时钟
- 《Windows程序设计》之数字时钟
- 计时器(2)之数字时钟
- js之图片数字时钟
- 自学QT之数字时钟
- iOS开发Xcode中导入Pch预编译文件
- UGUI基本属性设置
- linux文件的压缩解压缩
- 浅谈死锁破除和优化
- 面试题目总结2
- PyQt5 - QWidgets部件进阶教程之数字时钟
- 集成七牛云储存-上传图片Demo
- Gulp and Grunt
- 产品经理--2竞品分析
- zedgraph控件使用
- keepalived+twemproxy部署redis集群高可用
- 动态规划求最长公共子序列长度和子序列
- 某系统响应时间慢TPS低性能瓶颈调优过程
- 第4章 Android odex文件格式 第四节