PyQt5使用QTimer实现电子时钟

所属分类: 脚本专栏 / python 阅读数: 258
收藏 0 赞 0 分享

本文用 PyQt5 的QTimer类的两种方式实现电子时钟,供大家参考,具体内容如下

【效果图】

【知识点】

QTimer类提供了定时器信号/槽和单触发定时器。

它在内部使用定时器事件来提供更通用的定时器。

QTimer很容易使用:创建一个QTimer,使用start()来开始并且把它的timeout()连接到适当的槽。当这段时间过去了,它将会发射timeout()信号。

【实现】

1、定时器信号/槽方式

class MyTimer(QWidget):
  def __init__(self, parent = None):  
    # ......
    
    #新建一个QTimer对象    
    self.timer = QTimer()   
    self.timer.setInterval(1000)    
    self.timer.start()
     
    # 信号连接到槽    
    self.timer.timeout.connect(self.onTimerOut)

  # 定义槽
  def onTimerOut(self):    
    self.lcd.display(time.strftime("%X",time.localtime()))

完整代码:

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

class MyTimer(QWidget):
  def __init__(self, parent = None):
    super(MyTimer, self).__init__(parent)   
    self.resize(200, 100)   
    self.setWindowTitle("QTimerDemo")
    
    self.lcd = QLCDNumber()   
    self.lcd.setDigitCount(10)   
    self.lcd.setMode(QLCDNumber.Dec)
    self.lcd.setSegmentStyle(QLCDNumber.Flat)
    self.lcd.display(time.strftime("%X",time.localtime()))

    layout = QVBoxLayout()
    layout.addWidget(self.lcd)    
    self.setLayout(layout)
    
    #新建一个QTimer对象    
    self.timer = QTimer()   
    self.timer.setInterval(1000)    
    self.timer.start()
     
    # 信号连接到槽    
    self.timer.timeout.connect(self.onTimerOut)

  # 定义槽
  def onTimerOut(self):    
    self.lcd.display(time.strftime("%X",time.localtime()))


    
app = QApplication(sys.argv)
t = MyTimer()
t.show()
sys.exit(app.exec_())

2、定时器事件方式

class MyTimer(QWidget):
  def __init__(self, parent = None):
    # ......
    
    #新建一个QTimer对象    
    self.timer = QBasicTimer() # QTimer()貌似不行,不知何故?
    self.timer.start(1000, self) 
  
  # 覆写计时器事件处理函数timerEvent()
  def timerEvent(self, event):
    self.lcd.display(time.strftime("%X",time.localtime()))

完整代码:

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

class MyTimer(QWidget):
  def __init__(self, parent = None):
    super(MyTimer, self).__init__(parent)   
    self.resize(200, 100)   
    self.setWindowTitle("QTimerDemo")
    
    self.lcd = QLCDNumber()   
    self.lcd.setDigitCount(10)   
    self.lcd.setMode(QLCDNumber.Dec)
    self.lcd.setSegmentStyle(QLCDNumber.Flat)
    self.lcd.display(time.strftime("%X",time.localtime()))

    layout = QVBoxLayout()
    layout.addWidget(self.lcd)    
    self.setLayout(layout)
    
    #新建一个QTimer对象    
    self.timer = QBasicTimer() # QTimer()貌似不行,不知何故?
    self.timer.start(1000, self) 
  
  # 覆写计时器事件处理函数timerEvent()
  def timerEvent(self, event):
    if event.timerId() == self.timer.timerId():
      self.lcd.display(time.strftime("%X",time.localtime()))
    else:
      super(WigglyWidget, self).timerEvent(event)

    
app = QApplication(sys.argv)
t = MyTimer()
t.show()
sys.exit(app.exec_())

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Python中模块string.py详解

这篇文章主要介绍了Python中模块之string.py的相关资料,文中介绍的非常详细,对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

Python中关键字nonlocal和global的声明与解析

这篇文章主要给大家介绍了关于Python中关键字nonlocal和global的声明与解析的相关资料,文中介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

python中pandas.DataFrame对行与列求和及添加新行与列示例

pandas是python环境下最有名的数据统计包,而DataFrame翻译为数据框,是一种数据组织方式,这篇文章主要给大家介绍了python中pandas.DataFrame对行与列求和及添加新行与列的方法,文中给出了详细的示例代码,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

Python中str.format()详解

本文主要给大家详细介绍的是python编程中str.format()的基本语法和高级用法,非常的详细,并附有示例,希望大家能够喜欢
收藏 0 赞 0 分享

python中pandas.DataFrame的简单操作方法(创建、索引、增添与删除)

这篇文章主要介绍了python中pandas.DataFrame的简单操作方法,其中包括创建、索引、增添与删除等的相关资料,文中介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

Python IDLE 错误:IDLE''s subprocess didn''t make connection 的解决方案

这篇文章主要介绍了Python IDLE 错误:IDLE's subprocess didn't make connection 的解决方案的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Python中类型检查的详细介绍

Python是一种非常动态的语言,函数定义中完全没有类型约束。下面这篇文章主要给大家详细介绍了Python中类型检查的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

利用python程序生成word和PDF文档的方法

这篇文章主要给大家介绍了利用python程序生成word和PDF文档的方法,文中给出了详细的介绍和示例代码,相信对大家具有一定的参考价值,有需要的朋友们下面来一起看看吧。
收藏 0 赞 0 分享

python用装饰器自动注册Tornado路由详解

这篇文章主要给大家介绍了python用装饰器自动注册Tornado路由,文中给出了三个版本的解决方法,有需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

让python 3支持mysqldb的解决方法

这篇文章主要介绍了关于让python 3支持mysqldb的解决方法,文中给出解决的示例代码,相信对大家具有一定的参考价值,有需要的朋友可以一起来看看。
收藏 0 赞 0 分享
查看更多