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获取指定字符前面的所有字符方法

在用C和python编程时遇到的一个问题是: 用网口发送过来1k数据,数据格式是json,但是发送时不知道需要的大小,因为不同任务大小不一样,所以统一发送1024字节,统一接收1024了...

详细分析python3的reduce函数

详细分析python3的reduce函数

reduce() 函数在 python 2 是内置函数, 从python 3 开始移到了 functools 模块。 官方文档是这样介绍的 reduce(...) reduce(fu...

Python回文字符串及回文数字判定功能示例

本文实例讲述了Python回文字符串及回文数字判定功能。分享给大家供大家参考,具体如下: 所谓回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的。回文数字也是如此。 pyth...

Python 调用PIL库失败的解决方法

今天学习Python的时候,需要安装一个第三方库,Python Imaging Library,是Python下面一个非常强大的处理图像的工具库,不过PIL目前只支持到Python2.7...

用Python获取摄像头并实时控制人脸的实现示例

实现流程 从摄像头获取视频流,并转换为一帧一帧的图像,然后将图像信息传递给opencv这个工具库处理,返回灰度图像(就像你使用本地静态图片一样) 程序启动后,根据监听器信息,使用一个w...