|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在下方代码的基础上进行功能优化,
1.窗口可以拖动;
2.进入界面后是一个输入框,输入数值秒数后,回车开始倒计时;
import tkinter as tk
class CountdownTimer:
def __init__(self, duration):
self.duration = duration
self.root = tk.Tk()
self.root.overrideredirect(1) # 隐藏标题栏
self.root.attributes("-alpha", 0.8) # 设置透明度
self.root.attributes("-topmost", True) # 窗口置顶
# 获取屏幕宽高
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
# 计算窗口位置
x = int(screen_width / 2)
y = int(screen_height / 2)
# 创建标签并添加到窗口
self.label = tk.Label(self.root, text=self.get_time(), font=("Arial", 36))
self.label.pack()
# 设置窗口位置
self.root.geometry("+{}+{}".format(x, y))
# 更新倒计时
self.update_timer()
def update_timer(self):
if self.duration <= 0:
self.label.config(text="Time's up!")
self.root.after(2000, self.close_window) # 显示 "Time's up!" 两秒后关闭窗口
else:
self.label.config(text=self.get_time())
self.duration -= 1
self.root.after(1000, self.update_timer) # 每一秒更新一次倒计时
def get_time(self):
minutes = self.duration // 60
seconds = self.duration % 60
return "{:02d}:{:02d}".format(minutes, seconds)
def close_window(self):
self.root.destroy()
if __name__ == "__main__":
timer = CountdownTimer(180) # 设置倒计时时长为3分钟
# 获取窗口句柄
hwnd = int(timer.root.winfo_id())
# 进入事件循环
timer.root.mainloop()
要实现窗口的拖动功能,可以使用tkinter中的bind方法来绑定鼠标事件。具体步骤如下:
1. 在`__init__`函数中创建一个变量`self.drag_start_x`和`self.drag_start_y`,用于保存鼠标按下时窗口的初始位置。
- self.drag_start_x = 0
- self.drag_start_y = 0
复制代码
2. 在`__init__`函数中,为窗口绑定鼠标按下事件`<ButtonPress-1>`,并指定回调函数`self.on_drag_start`。
- self.root.bind("<ButtonPress-1>", self.on_drag_start)
复制代码
3. 实现回调函数`on_drag_start`,在该函数中保存鼠标按下时窗口的初始位置。
- def on_drag_start(self, event):
- self.drag_start_x = event.x
- self.drag_start_y = event.y
复制代码
4. 继续实现回调函数`on_drag_start`,为窗口绑定鼠标移动事件`<B1-Motion>`,并指定回调函数`self.on_drag_motion`。
- self.root.bind("<B1-Motion>", self.on_drag_motion)
复制代码
5. 实现回调函数`on_drag_motion`,在该函数中计算窗口应该移动的新位置,并调用`self.root.geometry`方法来设置窗口的新位置。
- 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))
复制代码
这样,当用户按下鼠标左键并移动时,窗口将跟随鼠标的移动而移动。
接下来是实现输入框功能的优化。可以使用tkinter中的`Entry`组件来创建一个输入框,并为其绑定回车事件。具体步骤如下:
1. 在`__init__`函数中创建一个`Entry`对象,并保存到`self.entry`变量中。
- self.entry = tk.Entry(self.root)
- self.entry.pack()
复制代码
2. 为输入框绑定回车事件`<Return>`,并指定回调函数`self.start_countdown`。
- self.entry.bind("<Return>", self.start_countdown)
复制代码
3. 实现回调函数`start_countdown`,在该函数中获取输入框中的数值,并将其转换为整型。
- def start_countdown(self, event):
- try:
- duration = int(self.entry.get())
- # 创建一个新的倒计时器对象
- countdown_timer = CountdownTimer(duration)
- except ValueError:
- # 处理非法输入的情况
- print("请输入一个有效的数值")
复制代码
这样,当用户在输入框中输入数值后按下回车,就会创建一个新的倒计时器对象,从而开始倒计时。
最终的代码如下:
- import tkinter as tk
- class CountdownTimer:
- def __init__(self, duration):
- self.duration = duration
- self.root = tk.Tk()
- self.root.overrideredirect(1) # 隐藏标题栏
- self.root.attributes("-alpha", 0.8) # 设置透明度
- self.root.attributes("-topmost", True) # 窗口置顶
- # 获取屏幕宽高
- screen_width = self.root.winfo_screenwidth()
- screen_height = self.root.winfo_screenheight()
- # 计算窗口位置
- x = int(screen_width / 2)
- y = int(screen_height / 2)
- # 创建标签并添加到窗口
- self.label = tk.Label(self.root, text=self.get_time(), font=("Arial", 36))
- self.label.pack()
- # 设置窗口位置
- self.root.geometry("+{}+{}".format(x, y))
- # 更新倒计时
- self.update_timer()
- # 创建输入框并添加到窗口
- self.entry = tk.Entry(self.root)
- self.entry.pack()
- # 绑定回车事件
- self.entry.bind("<Return>", self.start_countdown)
- # 绑定鼠标事件
- 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:
- self.duration -= 1
- self.label.config(text=self.get_time())
- self.root.after(1000, self.update_timer)
- else:
- self.root.destroy()
- 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, event):
- try:
- duration = int(self.entry.get())
- # 创建一个新的倒计时器对象
- countdown_timer = CountdownTimer(duration)
- except ValueError:
- # 处理非法输入的情况
- print("请输入一个有效的数值")
- countdown_timer = CountdownTimer(60)
- countdown_timer.root.mainloop()
复制代码
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|