❤️國慶假期快到了,用python寫個倒計時程式,助你熬到假期!❤️

来源:https://www.cnblogs.com/tuixiulaozhou/archive/2022/09/29/16743014.html
-Advertisement-
Play Games

國慶假期快到了,想查查還有幾天幾小時到假期,這對程式員小菜一碟,輕輕鬆松用python寫個倒計時程式(天、時、分、秒),助你熬到假期! 一、先看效果: 二、安裝python: 1、下載安裝python 下載安裝python3.9.6,進入python官方網站://www.python.org/ 點擊 ...


        國慶假期快到了,想查查還有幾天幾小時到假期,這對程式員小菜一碟,輕輕鬆松用python寫個倒計時程式(天、時、分、秒),助你熬到假期!

 

一、先看效果:

 

 

 

 二、安裝python:

 

1、下載安裝python

下載安裝python3.9.6,進入python官方網站://www.python.org/

   

 

 

 點擊Python 3.9.6

 

 

 

直接安裝即可。

 

2、驗證安裝成功。

 

按win+R輸入cmd,打開控制台,輸入python -V,輸出python版本號說明安裝成功。

 

 

 

三、代碼

##import library
from tkinter import *
import time
from datetime import datetime,timedelta
 
################GUI to display window ##########################
root = Tk()
root.geometry('450x300')
root.resizable(0,0)
root.config(bg ='blanched almond')
root.title('國慶倒計時')
Label(root, text = '國慶倒計時' , font = 'arial 20 bold', bg ='papaya whip').pack()
 
############GUI to display current time#######################
Label(root, font ='arial 15 bold', text = ' 當前時間:', bg = 'papaya whip').place(x = 40 ,y = 70)
 
#######################GUI to set the future time ##########
Label(root, font ='arial 15 bold', text = ' 到達時間:', bg = 'papaya whip').place(x = 40 ,y = 110)
#set year
year_set = StringVar()
Entry(root, textvariable =year_set , width = 4, font = 'arial 12').place(x=175, y=115)
Label(root, font ='arial 15', text = '-', bg = 'papaya whip').place(x = 215 ,y = 110)
year_set.set('0000')
 
#set month
month_set= StringVar()
Entry(root, textvariable =month_set, width =2, font = 'arial 12').place(x=235, y=115)
Label(root, font ='arial 15', text ='-', bg = 'papaya whip').place(x = 260 ,y = 110)
month_set.set('00')
 
#set day
day_set= StringVar()
Entry(root, textvariable =day_set, width =2, font = 'arial 12').place(x=275, y=115)
day_set.set('00')
 
# set hour
hour_set= StringVar()
Entry(root, textvariable =hour_set, width =2, font = 'arial 12').place(x=305, y=115)
Label(root, font ='arial 15', text = ':', bg = 'papaya whip').place(x = 330 ,y = 110)
hour_set.set('00')
 
# set min
min_set= StringVar()
Entry(root, textvariable =min_set, width =2, font = 'arial 12').place(x=345, y=115)
Label(root, font ='arial 15', text = ':', bg = 'papaya whip').place(x = 370 ,y = 110)
min_set.set('00')
 
# set sec
sec_set= StringVar()
Entry(root, textvariable =sec_set, width =2, font = 'arial 12').place(x=385, y=115)
sec_set.set('00')
 
#######################GUI to display timer countdown ##########
Label(root, font ='arial 15 bold', text = ' 倒計時:', bg ='papaya whip').place(x = 40 ,y = 150)
#storing seconds
sec = StringVar()
Entry(root, textvariable = sec, width = 2, font = 'arial 12').place(x=325, y=155)
Label(root, font ='arial 15', text = '', bg = 'papaya whip').place(x = 350 ,y = 150)
sec.set('00')
 
#storing minutes
mins= StringVar()
Entry(root, textvariable = mins, width =2, font = 'arial 12').place(x=275, y=155)
Label(root, font ='arial 15', text = '', bg = 'papaya whip').place(x = 300 ,y = 150)
mins.set('00')
 
# storing hours
hrs= StringVar()
Entry(root, textvariable = hrs, width =2, font = 'arial 12').place(x=225, y=155)
Label(root, font ='arial 15', text = '', bg = 'papaya whip').place(x = 250 ,y = 150)
hrs.set('00')
 
# storing days
days= StringVar()
Entry(root, textvariable = days, width =2, font = 'arial 12').place(x=175, y=155)
Label(root, font ='arial 15', text = '', bg = 'papaya whip').place(x = 200 ,y = 150)
days.set('00')
 
#########fun to display current time#############
def clock():
 clock_time = time.strftime('%Y-%m-%d %H:%M:%S %p')
 curr_time.config(text = clock_time)
 curr_time.after(1000,clock)
 
curr_time =Label(root, font ='arial 15 bold', text = '', fg = 'gray25' ,bg ='papaya whip')
curr_time.place(x = 175 , y = 70)
clock()
 
##########fun to start countdown########
def countdown():
 #now = datetime.now()
 #end = datetime((year_set).get(),(month_set).get(),(day_set).get(),(hour_set).get(),(min_set).get(),(sec_set).get(),00);
 global seconds_now
 now = time.time()
 lt_ = time.strptime(f'{(year_set).get()} {(month_set).get()} {(day_set).get()} {(hour_set).get()} {(min_set).get()} {(sec_set).get()}', '%Y %m %d %H %M %S')
 end = time.mktime(lt_)
 times=int (end-now)
  #.total_seconds());
 while times > -1:
  minute,second = (times // 60 , times % 60)
  
  hour = 0
  if minute > 60:
   hour , minute = (minute // 60 , minute % 60)
  
  day=0
  if hour>24:
    day,hour=(hour//24,hour%24) 
   
  sec.set(second)
  mins.set(minute)
  hrs.set(hour)
  days.set(day)
  root.update()
  time.sleep(1)
 
  times -= 1
 
Button(root, text='START', bd ='5', command = countdown, bg = 'antique white', font = 'arial 10 bold').place(x=150, y=210)  
root.mainloop()

四、運行

 

打開工程文件,在地址欄里輸入cmd,按Enter回車,即打開控制台。

 

 

 

 

 

 輸入python main.py,按回車就打開了程式GUI界面。

 

 

 

 

 

 到達時間填2022年10月1日,按start按鈕,就開始放假倒計時啦!

 
您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 隨著NFC讀寫器在BS架構下的需求越來越多,使用JS語言在web瀏覽器下操作NFC讀寫器就變得尤其重要.但是web瀏覽器不允許其顯示內容直接操作硬體,所以我們必須使用IC卡讀卡器web插件來實現這個功能.作為web前端工程師,我們首先要瞭解在web中實現操作NFC讀寫器的步驟:1、下載友我科技IC卡 ...
  • NullPointerException在開發過程中經常遇到,稍有不慎小BUG就出現了,如果避免這個問題呢,Optional就是專門解決這個問題的類,那麼Optional如何使用呢?讓我們一起探索一下吧! ...
  • 一、項目優化 1.去掉列印console 需求:在開發環境中,保留列印console;在生產上線環境,自動去掉列印console 使用步驟: 第一步:在項目根目錄下,創建如下圖兩個配置文件 在.env.development中(開發環境變數) NODE_ENV=development 在.env.p ...
  • 裝飾器模式(Decorator Design Pattern)是一種結構型設計模式,通過將對象放入包含行為的特殊封裝對象中來為原對象綁定新的行為。簡單地說,就是允許向一個現有的功能添加新的功能,同時又不改變其結構。 ...
  • 介面 interface 介面就是一組規範(就像我們法律一樣),所有實現類都要遵守。 面向對象的精髓,最能體現這一點的就是介面。為什麼我們討論設計模式都只針對具備了抽象能力的語言(比如 C++、Java、C#等),就是因為設計模式所研究的,實際上就是如何合理的去抽象。 介面的作用 為什麼需要介面?接 ...
  • ##示例代碼 public class ArrayListSource { public static void main(String[] args) { ArrayList arrayList = new ArrayList(); //跳轉至第一步 for (int i = 0; i < 10; ...
  • Java反射03 3.通過反射獲取類的結構信息 3.1java.lang.Class類 getName:獲取全類名 getSimpleName:獲取簡單類名 getFields:獲取所有public修飾的屬性,包含本類以及父類的 getDeclaredFields:獲取本類中所有屬性 getMeth ...
  • #(1)amax(),amin() 作用:計算數組中的元素沿指定軸的最大值,最小值 import numpy as np x = np.random.randint(1,11,9).reshape((3,3)) print(x) #output: [[ 9 1 2] [ 5 2 6] [10 10 ...
一周排行
    -Advertisement-
    Play Games
  • 概述:本文代碼示例演示瞭如何在WPF中使用LiveCharts庫創建動態條形圖。通過創建數據模型、ViewModel和在XAML中使用`CartesianChart`控制項,你可以輕鬆實現圖表的數據綁定和動態更新。我將通過清晰的步驟指南包括詳細的中文註釋,幫助你快速理解並應用這一功能。 先上效果: 在 ...
  • openGauss(GaussDB ) openGauss是一款全面友好開放,攜手伙伴共同打造的企業級開源關係型資料庫。openGauss採用木蘭寬鬆許可證v2發行,提供面向多核架構的極致性能、全鏈路的業務、數據安全、基於AI的調優和高效運維的能力。openGauss深度融合華為在資料庫領域多年的研 ...
  • openGauss(GaussDB ) openGauss是一款全面友好開放,攜手伙伴共同打造的企業級開源關係型資料庫。openGauss採用木蘭寬鬆許可證v2發行,提供面向多核架構的極致性能、全鏈路的業務、數據安全、基於AI的調優和高效運維的能力。openGauss深度融合華為在資料庫領域多年的研 ...
  • 概述:本示例演示了在WPF應用程式中實現多語言支持的詳細步驟。通過資源字典和數據綁定,以及使用語言管理器類,應用程式能夠在運行時動態切換語言。這種方法使得多語言支持更加靈活,便於維護,同時提供清晰的代碼結構。 在WPF中實現多語言的一種常見方法是使用資源字典和數據綁定。以下是一個詳細的步驟和示例源代 ...
  • 描述(做一個簡單的記錄): 事件(event)的本質是一個委托;(聲明一個事件: public event TestDelegate eventTest;) 委托(delegate)可以理解為一個符合某種簽名的方法類型;比如:TestDelegate委托的返回數據類型為string,參數為 int和 ...
  • 1、AOT適合場景 Aot適合工具類型的項目使用,優點禁止反編 ,第一次啟動快,業務型項目或者反射多的項目不適合用AOT AOT更新記錄: 實實在在經過實踐的AOT ORM 5.1.4.117 +支持AOT 5.1.4.123 +支持CodeFirst和非同步方法 5.1.4.129-preview1 ...
  • 總說周知,UWP 是運行在沙盒裡面的,所有許可權都有嚴格限制,和沙盒外交互也需要特殊的通道,所以從根本杜絕了 UWP 毒瘤的存在。但是實際上 UWP 只是一個應用模型,本身是沒有什麼許可權管理的,許可權管理全靠 App Container 沙盒控制,如果我們脫離了這個沙盒,UWP 就會放飛自我了。那麼有沒... ...
  • 目錄條款17:讓介面容易被正確使用,不易被誤用(Make interfaces easy to use correctly and hard to use incorrectly)限制類型和值規定能做和不能做的事提供行為一致的介面條款19:設計class猶如設計type(Treat class de ...
  • title: 從零開始:Django項目的創建與配置指南 date: 2024/5/2 18:29:33 updated: 2024/5/2 18:29:33 categories: 後端開發 tags: Django WebDev Python ORM Security Deployment Op ...
  • 1、BOM對象 BOM:Broswer object model,即瀏覽器提供我們開發者在javascript用於操作瀏覽器的對象。 1.1、window對象 視窗方法 // BOM Browser object model 瀏覽器對象模型 // js中最大的一個對象.整個瀏覽器視窗出現的所有東西都 ...