![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiYWan5SO2QDNzgTM2kTOyEjZ2UWM1Y2YwkTYldTMkljZ5kjZi9CX0JXZ252bj91Ztl2Lc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.gif)
為了感謝大家對“ Python客棧”的關注與支援,我們每天會在留言中随機抽取三位粉絲發放6.6元小紅包。快來參與吧!
長時間的久坐容易疲勞,也容易腰背部肌肉容易受損,平時工作學習的時候我們應該時不時的起來活動一會,不如就用python制作個簡單的倒計時提醒工具吧。
一、導入所需的子產品
from tkinter import *
import time
from playsound import playsound
tkinter用來制作圖形界面,time子產品用來顯示計算時間,playsound子產品用來設定提示音。
二、利用tkinter制作視窗
root = Tk()
root.geometry('400x300')
root.resizable(0,0)
root.title('倒計時')
root.mainloop()
- TK():初始化視窗
- geometry():設定視窗的大小
- resizable():鎖定視窗大小
- title():視窗标題
- mainloop():始終顯示視窗
三、建立目前時間
在視窗内建立一個标簽 用來提示目前時間
Label(root, font ='arial 15 bold ', text = '目前時間:', bg = 'Pink').place(x = 40 ,y = 70)
- font:字型參數
- text:要顯示的文本
- bg:标簽的背景顔色
- place():标簽的坐标位置
顯示目前時間
def clock():
clock_time = time.strftime('%H:%M:%S %p')
curr_time.config(text = clock_time)
curr_time.after(1000,clock)
curr_time =Label(root, font ='arial 15 bold', text = '' , bg ='pink')
curr_time.place(x = 190 , y = 70)
clock()
- strftime():用來格式化輸出時間,%H為24小時制小時數,%M為分鐘數,%s為秒數,%p用來提示a.m或p.m。
- after():設定1秒的時間更新。
四、實作倒計時功能
建立倒計時的輸入框
sec = StringVar()
Entry(root, textvariable = sec, width = 2, font = 'arial 12').place(x=250, y=155)
sec.set('00')
mins= StringVar()
Entry(root, textvariable = mins, width =2, font = 'arial 12').place(x=225, y=155)
mins.set('00')
hrs= StringVar()
Entry(root, textvariable = hrs, width =2, font = 'arial 12').place(x=200, y=155)
hrs.set('00')
- sec,mins,hrs:存儲秒,分,小時的字元串變量
- Entry():建立文本輸入框
- textvariable:和一個特定變量綁定
倒計時功能
def countdown():
times = int(hrs.get())*3600+ int(mins.get())*60 + int(sec.get())
while times > -1:
minute,second = (times // 60 , times % 60)
hour = 0
if minute > 60:
hour , minute = (minute // 60 , minute % 60)
sec.set(second)
mins.set(minute)
hrs.set(hour)
root.update()
time.sleep(1)
if(times == 0):
playsound('Loud_Alarm_Clock_Buzzer.mp3')
sec.set('00')
mins.set('00')
hrs.set('00')
times -= 1
time變量擷取總時間(小時*3600+分鐘*60+秒數),time大于-1時,倒計時執行,當time=0時,調用提示音(同目錄下的聲音檔案)。
補上倒計時前的标簽
Label(root, font ='arial 15 bold', text = '倒計時:', bg ='pink').place(x = 40 ,y = 150)
五、建立一個按鈕,啟用倒計時
Button(root, text='START', bd ='10', command = countdown, bg = 'pink', font = 'arial 10 bold').place(x=150, y=210)
- command:點選按鈕時,調用上面編寫好的倒計時函數
六、完整代碼
講解都在上面喽,就不寫注釋啦
from tkinter import *
import time
from playsound import playsound
root = Tk()
root.geometry('400x300')
root.resizable(0,0)
root.title('倒計時')
Label(root, font ='arial 15 bold ', text = '目前時間:', bg = 'Pink').place(x = 40 ,y = 70)
def clock():
clock_time = time.strftime('%H:%M:%S %p')
curr_time.config(text = clock_time)
curr_time.after(1000,clock)
curr_time =Label(root, font ='arial 15 bold', text = '' , bg ='pink')
curr_time.place(x = 190 , y = 70)
clock()
Label(root, font ='arial 15 bold', text = '倒計時', bg ='pink').place(x = 40 ,y = 150)
sec = StringVar()
Entry(root, textvariable = sec, width = 2, font = 'arial 12').place(x=250, y=155)
sec.set('00')
mins= StringVar()
Entry(root, textvariable = mins, width =2, font = 'arial 12').place(x=225, y=155)
mins.set('00')
hrs= StringVar()
Entry(root, textvariable = hrs, width =2, font = 'arial 12').place(x=200, y=155)
hrs.set('00')
def countdown():
times = int(hrs.get())*3600+ int(mins.get())*60 + int(sec.get())
while times > -1:
minute,second = (times // 60 , times % 60)
hour = 0
if minute > 60:
hour , minute = (minute // 60 , minute % 60)
sec.set(second)
mins.set(minute)
hrs.set(hour)
root.update()
time.sleep(1)
if(times == 0):
playsound('Loud_Alarm_Clock_Buzzer.mp3')
sec.set('00')
mins.set('00')
hrs.set('00')
times -= 1
Label(root, font ='arial 15 bold', text = '倒計時:', bg ='pink').place(x = 40 ,y = 150)
Button(root, text='START', bd ='10', command = countdown, bg = 'pink', font = 'arial 10 bold').place(x=150, y=210)
root.mainloop()
END
往期推薦
阿裡雲盤又雙叒叕上線啦!嘗鮮下載下傳
拒絕伸手!新手如何正确對待代碼報錯
Python 下載下傳檔案的七種方式,你get了嗎?
資深開發者都經常使用的10個 PyCharm 技巧
昨日留言中獎名單
以上三位小夥伴,快來聯系小編領取小小紅包一份哦!小編微信:Mayyy530
轉發,點贊,在看,安排一下?