基于Python實(shí)現(xiàn)新年倒計(jì)時(shí)
不知不覺(jué)已經(jīng)在家兩個(gè)月了,眼看馬上春節(jié)就要來(lái)臨了。滿懷期待的寫了一個(gè)新年倒計(jì)時(shí)的小工具!

設(shè)置新年時(shí)間后都能夠使用,打開(kāi)軟件后可以自動(dòng)計(jì)算到新年的倒計(jì)時(shí)情況。
UI界面及布局這塊一直使用的是PyQt5來(lái)實(shí)現(xiàn)的,若是沒(méi)有安裝使用pip的方式安裝一下即可。
pip?install?PyQt5
緊接著將PyQt5以及相關(guān)的模塊都導(dǎo)入到我們的代碼塊中準(zhǔn)備開(kāi)發(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ù)雜,我們這次不通過(guò)子線程來(lái)實(shí)現(xiàn)業(yè)務(wù)邏輯,直接在主線程中開(kāi)發(fā)界面布局和組件以及實(shí)現(xiàn)倒計(jì)時(shí)的過(guò)程。
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í)??公眾號(hào):Python 集中營(yíng)')
????????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):
????????"""
????????刷新頁(yè)面倒計(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_())
到此這篇關(guān)于基于Python實(shí)現(xiàn)新年倒計(jì)時(shí)的文章就介紹到這了,更多相關(guān)Python新年倒計(jì)時(shí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
keras 讀取多標(biāo)簽圖像數(shù)據(jù)方式
這篇文章主要介紹了keras 讀取多標(biāo)簽圖像數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
python matplotlib如何給圖中的點(diǎn)加標(biāo)簽
這篇文章主要介紹了python matplotlib給圖中的點(diǎn)加標(biāo)簽,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Keras實(shí)現(xiàn)DenseNet結(jié)構(gòu)操作
這篇文章主要介紹了Keras實(shí)現(xiàn)DenseNet結(jié)構(gòu)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
python不同版本的_new_不同點(diǎn)總結(jié)
在本篇內(nèi)容里小編給大家整理了一篇關(guān)于python不同版本的_new_不同點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-12-12

