基于Python實(shí)現(xiàn)新年倒計(jì)時(shí)
不知不覺已經(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í)??公眾號(hào):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_())
到此這篇關(guān)于基于Python實(shí)現(xiàn)新年倒計(jì)時(shí)的文章就介紹到這了,更多相關(guān)Python新年倒計(jì)時(shí)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python+Appium自動(dòng)化測試的實(shí)戰(zhàn)
本文主要介紹了Python+Appium自動(dòng)化測試的實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06python2利用wxpython生成投影界面工具的圖文詳解
這篇文章主要介紹了python2利用wxpython生成投影界面工具的圖文詳解,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04Windows和Linux下使用Python訪問SqlServer的方法介紹
這篇文章主要介紹了Windows和Linux下使用Python訪問SqlServer的方法介紹,本文講解了Windows下配置Python訪問Sqlserver、Linux下配置Python訪問SqlServer等內(nèi)容,需要的朋友可以參考下2015-03-03