|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在现有基础上,取消label的倒计时控件;
直接用文本框作为显示控件进行倒计时;
按钮功能保持不变
import tkinter as tk
class CountdownTimer:
def __init__(self):
self.is_counting = False
self.duration = 0
self.root = tk.Tk()
self.root.overrideredirect(1) # 隐藏标题栏
self.root.attributes("-alpha", 0.2) # 设置透明度
self.root.attributes("-topmost", True) # 窗口置顶
self.label = tk.Label(self.root, text=self.get_time(), font=("Arial", 36))
self.label.pack(side=tk.TOP)
self.reset_button = tk.Button(self.root, text="reset", bg='red', command=self.reset_countdown)
self.reset_button.pack(side=tk.RIGHT)
self.start_button = tk.Button(self.root, text="start", bg='green',command=self.start_countdown)
self.start_button.pack(side=tk.RIGHT)
self.entry = tk.Entry(self.root, width=8, font=("Arial", 16))
self.entry.pack(side=tk.RIGHT)
x = 1600
y = 50
self.root.geometry("+{}+{}".format(x, y))
self.update_timer()
self.drag_start_x = 0
self.drag_start_y = 0
self.root.bind("<Button-1>", self.on_drag_start)
self.root.bind("<B1-Motion>", self.on_drag_motion)
def update_timer(self):
if self.duration > 0 and self.is_counting:
self.duration -= 1
self.label.config(text=self.get_time())
self.root.after(1000, self.update_timer)
else:
self.is_counting = False
def get_time(self):
minutes = self.duration // 60
seconds = self.duration % 60
return "{:02d}:{:02d}".format(minutes, seconds)
def on_drag_start(self, event):
self.drag_start_x = event.x
self.drag_start_y = event.y
def on_drag_motion(self, event):
x = self.root.winfo_x() + (event.x - self.drag_start_x)
y = self.root.winfo_y() + (event.y - self.drag_start_y)
self.root.geometry("+{}+{}".format(x, y))
def start_countdown(self):
if not self.is_counting:
try:
duration = int(self.entry.get())
if duration > 0:
self.duration = duration
self.is_counting = True
self.update_timer()
else:
print("请输入一个正整数")
except ValueError:
print("请输入一个有效的数值")
def reset_countdown(self):
self.is_counting = False
self.duration = 0
self.label.config(text=self.get_time())
countdown_timer = CountdownTimer()
countdown_timer.root.mainloop()
好的,根据您的要求,我已经修改了代码,使用文本框代替了label作为显示倒计时的控件。现在您可以直接运行这段代码来看看效果。 import tkinter as tkclass CountdownTimer: def __init__(self): self.is_counting = False self.duration = 0 self.root = tk.Tk() self.root.overrideredirect(1) # 隐藏标题栏 self.root.attributes("-alpha", 0.2) # 设置透明度 self.root.attributes("-topmost", True) # 窗口置顶 self.textbox = tk.Entry(self.root, width=8, font=("Arial", 36)) self.textbox.pack(side=tk.TOP) self.reset_button = tk.Button(self.root, text="reset", bg='red', command=self.reset_countdown) self.reset_button.pack(side=tk.RIGHT) self.start_button = tk.Button(self.root, text="start", bg='green',command=self.start_countdown) self.start_button.pack(side=tk.RIGHT) self.entry = tk.Entry(self.root, width=8, font=("Arial", 16)) self.entry.pack(side=tk.RIGHT) x = 1600 y = 50 self.root.geometry("+{}+{}".format(x, y)) self.update_timer() self.drag_start_x = 0 self.drag_start_y = 0 self.root.bind("<ButtonPress-1>", self.on_drag_start) self.root.bind("<B1-Motion>", self.on_drag_motion) def update_timer(self): if self.duration > 0 and self.is_counting: self.duration -= 1 self.textbox.delete(0, tk.END) # 清空文本框内容 self.textbox.insert(tk.END, self.get_time()) # 在文本框插入新的倒计时时间 self.root.after(1000, self.update_timer) else: self.is_counting = False def get_time(self): minutes = self.duration // 60 seconds = self.duration % 60 return "{:02d}:{:02d}".format(minutes, seconds) def on_drag_start(self, event): self.drag_start_x = event.x self.drag_start_y = event.y def on_drag_motion(self, event): x = self.root.winfo_x() + (event.x - self.drag_start_x) y = self.root.winfo_y() + (event.y - self.drag_start_y) self.root.geometry("+{}+{}".format(x, y)) def start_countdown(self): if not self.is_counting: try: duration = int(self.entry.get()) if duration > 0: self.duration = duration self.is_counting = True self.update_timer() else: print("请输入一个正整数") except ValueError: print("请输入一个有效的数值") def reset_countdown(self): self.is_counting = False self.duration = 0 self.textbox.delete(0, tk.END) self.textbox.insert(tk.END, self.get_time())countdown_timer = CountdownTimer()countdown_timer.root.mainloop()
希望这次修改符合您的要求,如果还有其他问题,请随时告诉我。祝您编程愉快!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|