Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python to write a countdown program for National Day holiday

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces how to use python to write a National Day holiday countdown program, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.

First, let's look at the results:

Install python:1, download and install python

Download and install python3.9.6 and go to the official website of python: http://www.python.org/

Click Python 3.9.6

It can be installed directly.

2. Verify the installation is successful.

Press win+R to enter cmd, open the console, enter python-V, and output the python version number indicating that the installation is successful.

Code # # import libraryfrom tkinter import * import timefrom datetime import datetime,timedelta # GUI to display window # # root = Tk () root.geometry ('450x300') root.resizable (0P0) root.config (bg =' blanched almond') root.title ('National Day countdown') Label (root, text = 'National Day countdown', font = 'arial 20 bold'' Bg = 'papaya whip'). Pack () # GUI to display current time###Label (root, font =' arial 15 bold', text = 'current time:', bg = 'papaya whip') .place (x = 40 Y = 70) # GUI to set the future time # Label (root, font = 'arial 15 bold', text =' time of arrival:', bg = 'papaya whip') .place (x = 40, y = 110) # set yearyear_set = StringVar () Entry (root, textvariable = year_set, width = 4, font =' arial 12'). Place (root, font = 'arial 15') Label Text ='-', bg = 'papaya whip') .place (x = 215,110) year_set.set (' 0000') # set monthmonth_set= StringVar () Entry (root, textvariable = month_set, width = 2, font = 'arial 12') .place (root, font =' arial 15', text ='-', bg = 'papaya whip') .place (x = 260,110) month_set.set (' 00') # set dayday_set= StringVar () Entry (root, textvariable = day_set) Width = 2, font = 'arial 12') .place (x = 275, y = 115) day_set.set (' 00') # set hourhour_set= StringVar () Entry (root, textvariable = hour_set, width = 2, font = 'arial 12') .place (x = 'arial 305, y = 115) Label (root, font =' arial 15, text =':', bg = 'papaya whip') .place (x = 330, y = 110) hour_set.set (' 00') # set minmin_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 secsec_set= StringVar () Entry (root, textvariable = sec_set, width = 2, font = 'arial 12') .place (x = 385) Yellow115) sec_set.set ('00') # GUI to display timer countdown # Label (root, font =' arial 15 bold', text = 'countdown:', bg = 'papaya whip') .place (x = 40, y = 150) # storing secondssec = StringVar () Entry (root, textvariable = sec, width = 2, font =' arial 12'). Place (xtriple 325, yellow155) Label (root Font = 'arial 15 seconds, text =' seconds', bg = 'papaya whip') .place (x = 350,150) sec.set (' 00') # storing minutesmins= StringVar () Entry (root, textvariable = mins, width = 2, font = 'arial 12') .place (root, font =' arial 155s, text = 'minutes', bg = 'papaya whip') .place (x = 300,150) mins.set (' 00') # storing hourshrs= StringVar () Entry (root, textvariable = hrs, width = 2) Font = 'arial 12') .place (x = 225,155) Label (root, font =' arial 15', text = 'hour', bg = 'papaya whip') .place (x = 250,150) hrs.set (' 00') # storing daysdays= StringVar () Entry (root, textvariable = days, width = 2, font = 'arial 12') .place (x = 175,155) Label (root, font =' arial 15', text = 'days', 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 = 175175y = 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 (1) times-= 1 Button (root, text='START', bd ='5' Command = countdown, bg = 'antique white', font =' arial 10 bold') .place (xylene 150, yearly 210) root.mainloop () 4. Run

Open the project file, type cmd in the address bar, press Enter enter, and then open the console.

Enter python main.py and press enter to open the program GUI interface.

Fill in the arrival time on October 1st, 2021, press the start button, and the countdown to the holiday begins!

Thank you for reading this article carefully. I hope the article "how to use python to write a countdown program for National Day holiday" shared by the editor will be helpful to everyone. At the same time, I also hope you can support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report