Tkinter는 GUI 기반 데스크톱 응용 프로그램을 만들기 위한 표준 Python 라이브러리입니다. 응용 프로그램의 기능을 구현하는 데 사용할 수 있는 다양한 기능, 모듈 및 메서드를 제공합니다.
이 예제에서는 Tkinter 및 time 모듈과 같은 Python 표준 라이브러리를 사용하여 카운트다운 시간을 생성합니다. 우리 애플리케이션의 기본 기능은 주어진 시간 동안 타이머를 실행하는 것입니다. 다음 구성 요소가 있습니다.
-
HH/MM/SS에 대해 각각 타이머를 설정하는 항목 위젯.
-
countdowntimer() 기능을 실행하는 버튼 .
-
countdowntimer() 함수 입력 문자열을 HH, MM 및 SS에 상대적인 정수 값으로 변환합니다.
-
update() 사용 메서드를 사용하면 주어진 기능과 위젯에 대해 창을 업데이트합니다.
예시
# Import the required library
from tkinter import *
import time
# Create an instance of tkinter frame
win = Tk()
# Set the size of the window
win.geometry('700x350')
# Make the window fixed to its size
win.resizable(False, False)
# Configure the background
win.config(bg='skyblue4')
# Create Entry Widgets for HH MM SS
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')
# Define the function for the timer
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)
# Update the time
win.update()
time.sleep(1)
if (times == 0):
sec.set('00')
mins.set('00')
hrs.set('00')
times -= 1
# Create a Label widget
Label(win, font=('Helvetica bold', 22), text='Set the Timer', bg='skyblue4', fg="white").place(x=260, y=70)
# Button widget to set the timer
Button(win, text='START', bd='2', bg='IndianRed1', font=('Helvetica bold', 10), command=countdowntimer).place(x=335, y=180)
win.mainloop() 출력
창에 카운트다운 타이머가 표시됩니다.

입력 상자의 값을 변경하여 타이머를 설정하고 "시작" 버튼을 클릭하면 지정된 시간 동안 타이머가 빠르게 시작됩니다.
