基于Python实现新年倒计时

作者:Sir 时间:2022-02-14 08:41:59 

不知不觉已经在家两个月了,眼看马上春节就要来临了。满怀期待的写了一个新年倒计时的小工具!

基于Python实现新年倒计时

设置新年时间后都能够使用,打开软件后可以自动计算到新年的倒计时情况。

UI界面及布局这块一直使用的是PyQt5来实现的,若是没有安装使用pip的方式安装一下即可。

pip install PyQt5

紧接着将PyQt5以及相关的模块都导入到我们的代码块中准备开发。

# Importing all the classes from the QtWidgets module.
from PyQt5.QtWidgets import *

# Importing all the classes from the QtGui module.
from PyQt5.QtGui import *

# Importing the sys module.
import sys

# It imports all the classes from the QtCore module.
from PyQt5.QtCore import *

# Importing the datetime module.
import datetime

# Importing the math module.
import math

业务逻辑不是很复杂,我们这次不通过子线程来实现业务逻辑,直接在主线程中开发界面布局和组件以及实现倒计时的过程。

class CountDown(QWidget):
    def __init__(self):
        """
        A constructor. It is called when an object is created from a class and it allows the class to initialize the
        attributes of a class.
        """
        super(CountDown, self).__init__()
        self.spring_date = datetime.datetime(2023, 1, 21, 0, 0, 0)
        self.init_code()

    def init_code(self):
        """
        业务初始化代码块
        """
        self.setWindowTitle('新年倒计时  公众号:Python 集中营')
        self.setWindowIcon(QIcon('倒计时.png'))
        self.resize(600, 400)

        palette = QPalette()
        palette.setBrush(QPalette.Background, QBrush(QPixmap("./背景.jpeg")))
        self.setPalette(palette)

        self.timer = QTimer()
        self.timer.setInterval(1000)
        self.timer.timeout.connect(self.refresh_count_down)
        self.timer.start()

        self.time_label = QLabel()
        self.time_label.setAlignment(Qt.AlignCenter)
        self.time_label.setStyleSheet('color:red;font:bold 28px;font-family:黑体')

        vbox = QVBoxLayout()
        vbox.addWidget(self.time_label)

        self.setLayout(vbox)

    def refresh_count_down(self):
        """
        刷新页面倒计时时间
        """
        current_date = datetime.datetime.now()
        differ_day = (self.spring_date - current_date).days
        seconds = (self.spring_date - current_date).seconds
        differ_second = seconds % 60
        differ_minute = seconds / 60 % 60
        differ_hour = seconds / 60 / 60
        if differ_hour > 24:
            differ_hour = differ_hour - 24

        differ_hour = math.floor(differ_hour)
        differ_minute = math.floor(differ_minute)

        self.time_label.setText(
            "距离春节:" + str(differ_day) + "天" + str(differ_hour) + "小时" + str(differ_minute) + "分钟" + str(
                differ_second) + "秒" + '\r')

基于Python实现新年倒计时

使用python模块主函数直接启动春节倒计时桌面应用,就大功告成啦!

# A special variable in Python that evaluates to `True` if the module is being run as the main program.
if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = CountDown()
    main.show()
    sys.exit(app.exec_())

来源:https://mp.weixin.qq.com/s/rYAdeqsv6Dae--hx_aoRqA

标签:Python,倒计时
0
投稿

猜你喜欢

  • go语言题解LeetCode1275找出井字棋的获胜者示例

    2023-07-22 12:30:41
  • 去掉CSS赘余代码,CSS可以更简洁

    2008-11-05 13:07:00
  • React TypeScript 应用中便捷使用Redux Toolkit方法详解

    2023-08-11 09:48:21
  • Yii2结合Workerman的websocket示例详解

    2023-11-17 04:48:40
  • python os.listdir()乱码解决方案

    2021-09-20 02:52:42
  • 如何防止Application对象在多线程访问中出现错误?

    2009-11-22 19:18:00
  • 用Dreamweaver设计自动关闭的网页

    2010-09-02 12:29:00
  • PHP的Yii框架中行为的定义与绑定方法讲解

    2023-07-21 20:42:33
  • Python编程生成随机用户名及密码的方法示例

    2021-12-14 13:12:17
  • Sql server中的char、varchar、text和nchar、nvarchar、ntext的区别

    2011-08-14 09:43:44
  • Python3利用Dlib实现摄像头实时人脸检测和平铺显示示例

    2021-12-14 16:37:30
  • python利用正则表达式搜索单词示例代码

    2023-02-21 11:31:03
  • ajax+asp无限级分类树型结构

    2011-04-02 11:05:00
  • MySQL数据库安全解决方案

    2009-10-17 21:36:00
  • python 数据提取及拆分的实现代码

    2023-11-13 09:13:12
  • 一个导航的前端实现

    2008-11-13 12:41:00
  • 关于长度单位pt、px、dpi的误解

    2008-06-01 13:30:00
  • 用ASP和XMLHTTP分析远程XML文件

    2007-12-12 12:48:00
  • Python调用百度AI实现颜值评分功能

    2023-07-30 22:53:40
  • 十幅图告诉你什么是PHP引用

    2023-10-04 06:16:56
  • asp之家 网络编程 m.aspxhome.com