打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用python一起进入新年倒计时吧,可直接打包成exe应用!

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

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

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(2023121000)
        self.init_code()

    def init_code(self):
        """
        业务初始化代码块
        """

        self.setWindowTitle('新年倒计时  公众号:Python 集中营')
        self.setWindowIcon(QIcon('倒计时.png'))
        self.resize(600400)

        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模块主函数直接启动春节倒计时桌面应用,就大功告成啦!

# 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_())

「Python 集中营」,只做知识分享 !

python数据清洗:pandas如何完成对Excel数据类型的转换!

python数据可视化:数据分析后的词云图片生成!

推荐windows命令行软件管理工具WinGet,相当方便!

如何使用Selenium IDE浏览器插件轻松完成脚本录制,轻松搞定自动化测试!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
PyQt信号与槽之多窗口数据传递(七)
Python Rapid GUI Programming 第二篇。 30行写一个更奇葩的计...
Python PyQt5整理介绍
新手写给新手的PyQt 5开发心得(上)
基于Pyqt5打造量化系统GUI
PyQt5无边框窗口的标题拖动和窗口缩放实现
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服