Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Tạo đồng hồ đếm ngược (countdown timer) với Python và Tkinter


Xây dựng ứng dụng đếm ngược giờ:phút:giây bằng Tkinter và module time.

  • Ba Entry cho giờ, phút, giây (StringVar).
  • Nút START gọi hàm countdowntimer().
  • Hàm chuyển đổi thời gian sang tổng giây, giảm dần mỗi giây, cập nhật giao diện bằng win.update() và time.sleep(1).

Ví dụ minh họa

from tkinter import *
import time

win = Tk()
win.geometry('700x350')
win.resizable(False, False)
win.config(bg='skyblue4')

sec = StringVar()
Entry(win, textvariable=sec, width=2, font='Helvetica 14').place(x=380, y=120)
sec.set('00')

mins = StringVar()
Entry(win, textvariable=mins, width=2, font='Helvetica 14').place(x=346, y=120)
mins.set('00')

hrs = StringVar()
Entry(win, textvariable=hrs, width=2, font='Helvetica 14').place(x=310, y=120)
hrs.set('00')

def countdowntimer():
    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)
        win.update()
        time.sleep(1)
        if times == 0:
            sec.set('00')
            mins.set('00')
            hrs.set('00')
        times -= 1

Label(win, font=('Helvetica bold', 22), text='Đặt thời gian', bg='skyblue4', fg="white").place(x=260, y=70)
Button(win, text='START', bd='2', bg='IndianRed1', font=('Helvetica bold', 10), command=countdowntimer).place(x=335, y=180)

win.mainloop()

Kết quả đầu ra

Giao diện đếm ngược.

Tạo đồng hồ đếm ngược (countdown timer) với Python và Tkinter

Đặt thời gian và nhấn START.

Tạo đồng hồ đếm ngược (countdown timer) với Python và Tkinter