Files
weipan_cl/scripts/testui.py
2025-02-12 11:46:33 +08:00

51 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import tkinter as tk
from tkinter import ttk
# 创建主窗口
root = tk.Tk()
root.title("倒计时示例")
# 设置窗口的初始尺寸
root.geometry("400x300") # 设置为 400x300 像素
# 设置窗口的最小尺寸
root.minsize(300, 200) # 最小尺寸为 300x200 像素
# 设置窗口的最大尺寸
root.maxsize(500, 400) # 最大尺寸为 500x400 像素
# 创建一个 Frame
frame = ttk.Frame(root)
frame.pack(padx=10, pady=10)
# 创建一个 Label 用于显示倒计时
countdown_label = ttk.Label(frame, text="倒计时10")
countdown_label.grid(row=0, column=0, pady=10)
# 定义一个倒计时函数
def countdown():
global count
if count > 0:
countdown_label.config(text=f"倒计时:{count}")
print(f"倒计时:{count}")
count -= 1
root.after(1000, countdown) # 每隔1秒更新一次
else:
countdown_label.config(text="倒计时结束")
# 初始化倒计时变量
count = 10
# 定义一个函数,用于启动倒计时
def start_countdown():
global count
count = 10 # 重置倒计时
countdown()
# 创建按钮,并绑定点击事件
start_button = ttk.Button(frame, text="开始倒计时", command=start_countdown)
start_button.grid(row=1, column=0, pady=5)
# 启动主事件循环
root.mainloop()