❤️國慶假期快到了,用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
  • 前言 在我們開發過程中基本上不可或缺的用到一些敏感機密數據,比如SQL伺服器的連接串或者是OAuth2的Secret等,這些敏感數據在代碼中是不太安全的,我們不應該在源代碼中存儲密碼和其他的敏感數據,一種推薦的方式是通過Asp.Net Core的機密管理器。 機密管理器 在 ASP.NET Core ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 順序棧的介面程式 目錄順序棧的介面程式頭文件創建順序棧入棧出棧利用棧將10進位轉16進位數驗證 頭文件 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> 創建順序棧 // 指的是順序棧中的元素的數據類型,用戶可以根據需要進行修改 ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • C總結與剖析:關鍵字篇 -- <<C語言深度解剖>> 目錄C總結與剖析:關鍵字篇 -- <<C語言深度解剖>>程式的本質:二進位文件變數1.變數:記憶體上的某個位置開闢的空間2.變數的初始化3.為什麼要有變數4.局部變數與全局變數5.變數的大小由類型決定6.任何一個變數,記憶體賦值都是從低地址開始往高地 ...
  • 如果讓你來做一個有狀態流式應用的故障恢復,你會如何來做呢? 單機和多機會遇到什麼不同的問題? Flink Checkpoint 是做什麼用的?原理是什麼? ...
  • C++ 多級繼承 多級繼承是一種面向對象編程(OOP)特性,允許一個類從多個基類繼承屬性和方法。它使代碼更易於組織和維護,並促進代碼重用。 多級繼承的語法 在 C++ 中,使用 : 符號來指定繼承關係。多級繼承的語法如下: class DerivedClass : public BaseClass1 ...
  • 前言 什麼是SpringCloud? Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發便利性簡化了分散式系統的開發,比如服務註冊、服務發現、網關、路由、鏈路追蹤等。Spring Cloud 並不是重覆造輪子,而是將市面上開發得比較好的模塊集成進去,進行封裝,從 ...
  • class_template 類模板和函數模板的定義和使用類似,我們已經進行了介紹。有時,有兩個或多個類,其功能是相同的,僅僅是數據類型不同。類模板用於實現類所需數據的類型參數化 template<class NameType, class AgeType> class Person { publi ...
  • 目錄system v IPC簡介共用記憶體需要用到的函數介面shmget函數--獲取對象IDshmat函數--獲得映射空間shmctl函數--釋放資源共用記憶體實現思路註意 system v IPC簡介 消息隊列、共用記憶體和信號量統稱為system v IPC(進程間通信機制),V是羅馬數字5,是UNI ...