python实现弹窗倒计时
时间: 2024-09-30 13:14:54 浏览: 27
元旦倒计时Python代码
在Python中,你可以使用`tkinter`库来创建简单的图形用户界面,包括弹窗和倒计时功能。以下是一个简化的示例,展示如何在一个窗口上显示倒计时:
```python
import tkinter as tk
from time import sleep
def countdown(t):
while t > 0:
mins, secs = divmod(t, 60)
timer.config(text=f"{mins:02d}:{secs:02d}")
root.update()
sleep(1)
t -= 1
def start_countdown():
global t
t = int(entry.get())
if t <= 0:
return
thread = threading.Thread(target=countdown, args=(t,))
thread.start()
root = tk.Tk()
root.geometry("250x100")
timer = tk.Label(root, font=("Arial", 48), anchor="center")
timer.pack(pady=20)
start_button = tk.Button(root, text="开始倒计时", command=start_countdown)
start_button.pack()
entry = tk.Entry(root)
entry.pack()
root.mainloop()
阅读全文