不知不覺已經(jīng)在家兩個(gè)月了,眼看馬上春節(jié)就要來臨了。滿懷期待的寫了一個(gè)新年倒計(jì)時(shí)的小工具!
設(shè)置新年時(shí)間后都能夠使用,打開軟件后可以自動(dòng)計(jì)算到新年的倒計(jì)時(shí)情況。
UI界面及布局這塊一直使用的是PyQt5來實(shí)現(xiàn)的,若是沒有安裝使用pip的方式安裝一下即可。
pip install PyQt5
緊接著將PyQt5以及相關(guān)的模塊都導(dǎo)入到我們的代碼塊中準(zhǔn)備開發(fā)。
# 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
業(yè)務(wù)邏輯不是很復(fù)雜,我們這次不通過子線程來實(shí)現(xiàn)業(yè)務(wù)邏輯,直接在主線程中開發(fā)界面布局和組件以及實(shí)現(xiàn)倒計(jì)時(shí)的過程。
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):
"""
業(yè)務(wù)初始化代碼塊
"""
self.setWindowTitle('新年倒計(jì)時(shí) 公眾號:Python 集中營')
self.setWindowIcon(QIcon('倒計(jì)時(shí).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):
"""
刷新頁面倒計(jì)時(shí)時(shí)間
"""
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(
"距離春節(jié):" + str(differ_day) + "天" + str(differ_hour) + "小時(shí)" + str(differ_minute) + "分鐘" + str(
differ_second) + "秒" + '\r')
使用python模塊主函數(shù)直接啟動(dòng)春節(jié)倒計(jì)時(shí)桌面應(yīng)用,就大功告成啦!
# 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數(shù)據(jù)清洗:pandas如何完成對Excel數(shù)據(jù)類型的轉(zhuǎn)換!
聯(lián)系客服