PyQt5使用QTimer实现电子时钟

yipeiwu_com6年前Python基础

本文用 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设计】。

相关文章

Python内置函数Type()函数一个有趣的用法

今天在网上看到type的一段代码 ,然后查了一下文档,才知道type还有三个参数的用法。 http://docs.python.org/2/library/functions.html...

Python3非对称加密算法RSA实例详解

本文实例讲述了Python3非对称加密算法RSA。分享给大家供大家参考,具体如下: python3 可以使用 Crypto.PublicKey.RSA 和 rsa 生成公钥、私钥。 其中...

春节到了 教你使用python来抢票回家

这篇文章主要介绍了春节到了 教你使用python来抢票回家,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 不知不觉,一年一度的春运抢票...

Python cookbook(数据结构与算法)对切片命名清除索引的方法

本文实例讲述了Python对切片命名清除索引的方法。分享给大家供大家参考,具体如下: 问题:如何清理掉到处都是硬编码的切片索引 解决方案:对切片命名 假设有一些代码用来从字符串的固定位置...

Python中实现输入超时及如何通过变量获取变量名

Python中实现输入超时及如何通过变量获取变量名

背景介绍 开发中遇到了一个需求:程序运行到某处时需要用户确认, 但不能一直傻等, 后面的程序不能被一直阻塞, 需要有个超时限制, 也就是这个程序如果在一段时间后还没有得到用户输入就执行...