鱼C论坛

 找回密码
 立即注册
查看: 1564|回复: 1

[已解决]悬浮倒计时钟优化

[复制链接]
发表于 2023-12-14 21:46:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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()
最佳答案
2023-12-14 21:47:19
要实现窗口的拖动功能,可以使用tkinter中的bind方法来绑定鼠标事件。具体步骤如下:

1. 在`__init__`函数中创建一个变量`self.drag_start_x`和`self.drag_start_y`,用于保存鼠标按下时窗口的初始位置。
  1. self.drag_start_x = 0
  2. self.drag_start_y = 0
复制代码

2. 在`__init__`函数中,为窗口绑定鼠标按下事件`<ButtonPress-1>`,并指定回调函数`self.on_drag_start`。
  1. self.root.bind("<ButtonPress-1>", self.on_drag_start)
复制代码

3. 实现回调函数`on_drag_start`,在该函数中保存鼠标按下时窗口的初始位置。
  1. def on_drag_start(self, event):
  2.     self.drag_start_x = event.x
  3.     self.drag_start_y = event.y
复制代码

4. 继续实现回调函数`on_drag_start`,为窗口绑定鼠标移动事件`<B1-Motion>`,并指定回调函数`self.on_drag_motion`。
  1. self.root.bind("<B1-Motion>", self.on_drag_motion)
复制代码

5. 实现回调函数`on_drag_motion`,在该函数中计算窗口应该移动的新位置,并调用`self.root.geometry`方法来设置窗口的新位置。
  1. def on_drag_motion(self, event):
  2.     x = self.root.winfo_x() + (event.x - self.drag_start_x)
  3.     y = self.root.winfo_y() + (event.y - self.drag_start_y)
  4.     self.root.geometry("+{}+{}".format(x, y))
复制代码

这样,当用户按下鼠标左键并移动时,窗口将跟随鼠标的移动而移动。

接下来是实现输入框功能的优化。可以使用tkinter中的`Entry`组件来创建一个输入框,并为其绑定回车事件。具体步骤如下:

1. 在`__init__`函数中创建一个`Entry`对象,并保存到`self.entry`变量中。
  1. self.entry = tk.Entry(self.root)
  2. self.entry.pack()
复制代码

2. 为输入框绑定回车事件`<Return>`,并指定回调函数`self.start_countdown`。
  1. self.entry.bind("<Return>", self.start_countdown)
复制代码

3. 实现回调函数`start_countdown`,在该函数中获取输入框中的数值,并将其转换为整型。
  1. def start_countdown(self, event):
  2.     try:
  3.         duration = int(self.entry.get())
  4.         # 创建一个新的倒计时器对象
  5.         countdown_timer = CountdownTimer(duration)
  6.     except ValueError:
  7.         # 处理非法输入的情况
  8.         print("请输入一个有效的数值")
复制代码

这样,当用户在输入框中输入数值后按下回车,就会创建一个新的倒计时器对象,从而开始倒计时。

最终的代码如下:
  1. import tkinter as tk
  2. class CountdownTimer:
  3.     def __init__(self, duration):
  4.         self.duration = duration
  5.         self.root = tk.Tk()
  6.         self.root.overrideredirect(1)  # 隐藏标题栏
  7.         self.root.attributes("-alpha", 0.8)  # 设置透明度
  8.         self.root.attributes("-topmost", True)  # 窗口置顶
  9.         # 获取屏幕宽高
  10.         screen_width = self.root.winfo_screenwidth()
  11.         screen_height = self.root.winfo_screenheight()
  12.         # 计算窗口位置
  13.         x = int(screen_width / 2)
  14.         y = int(screen_height / 2)
  15.         # 创建标签并添加到窗口
  16.         self.label = tk.Label(self.root, text=self.get_time(), font=("Arial", 36))
  17.         self.label.pack()
  18.         # 设置窗口位置
  19.         self.root.geometry("+{}+{}".format(x, y))
  20.         # 更新倒计时
  21.         self.update_timer()
  22.         # 创建输入框并添加到窗口
  23.         self.entry = tk.Entry(self.root)
  24.         self.entry.pack()
  25.         # 绑定回车事件
  26.         self.entry.bind("<Return>", self.start_countdown)
  27.         # 绑定鼠标事件
  28.         self.drag_start_x = 0
  29.         self.drag_start_y = 0
  30.         self.root.bind("<ButtonPress-1>", self.on_drag_start)
  31.         self.root.bind("<B1-Motion>", self.on_drag_motion)
  32.     def update_timer(self):
  33.         if self.duration > 0:
  34.             self.duration -= 1
  35.             self.label.config(text=self.get_time())
  36.             self.root.after(1000, self.update_timer)
  37.         else:
  38.             self.root.destroy()
  39.     def get_time(self):
  40.         minutes = self.duration // 60
  41.         seconds = self.duration % 60
  42.         return "{:02d}:{:02d}".format(minutes, seconds)
  43.     def on_drag_start(self, event):
  44.         self.drag_start_x = event.x
  45.         self.drag_start_y = event.y
  46.     def on_drag_motion(self, event):
  47.         x = self.root.winfo_x() + (event.x - self.drag_start_x)
  48.         y = self.root.winfo_y() + (event.y - self.drag_start_y)
  49.         self.root.geometry("+{}+{}".format(x, y))
  50.     def start_countdown(self, event):
  51.         try:
  52.             duration = int(self.entry.get())
  53.             # 创建一个新的倒计时器对象
  54.             countdown_timer = CountdownTimer(duration)
  55.         except ValueError:
  56.             # 处理非法输入的情况
  57.             print("请输入一个有效的数值")
  58. countdown_timer = CountdownTimer(60)
  59. countdown_timer.root.mainloop()
复制代码


以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-12-14 21:47:19 | 显示全部楼层    本楼为最佳答案   
要实现窗口的拖动功能,可以使用tkinter中的bind方法来绑定鼠标事件。具体步骤如下:

1. 在`__init__`函数中创建一个变量`self.drag_start_x`和`self.drag_start_y`,用于保存鼠标按下时窗口的初始位置。
  1. self.drag_start_x = 0
  2. self.drag_start_y = 0
复制代码

2. 在`__init__`函数中,为窗口绑定鼠标按下事件`<ButtonPress-1>`,并指定回调函数`self.on_drag_start`。
  1. self.root.bind("<ButtonPress-1>", self.on_drag_start)
复制代码

3. 实现回调函数`on_drag_start`,在该函数中保存鼠标按下时窗口的初始位置。
  1. def on_drag_start(self, event):
  2.     self.drag_start_x = event.x
  3.     self.drag_start_y = event.y
复制代码

4. 继续实现回调函数`on_drag_start`,为窗口绑定鼠标移动事件`<B1-Motion>`,并指定回调函数`self.on_drag_motion`。
  1. self.root.bind("<B1-Motion>", self.on_drag_motion)
复制代码

5. 实现回调函数`on_drag_motion`,在该函数中计算窗口应该移动的新位置,并调用`self.root.geometry`方法来设置窗口的新位置。
  1. def on_drag_motion(self, event):
  2.     x = self.root.winfo_x() + (event.x - self.drag_start_x)
  3.     y = self.root.winfo_y() + (event.y - self.drag_start_y)
  4.     self.root.geometry("+{}+{}".format(x, y))
复制代码

这样,当用户按下鼠标左键并移动时,窗口将跟随鼠标的移动而移动。

接下来是实现输入框功能的优化。可以使用tkinter中的`Entry`组件来创建一个输入框,并为其绑定回车事件。具体步骤如下:

1. 在`__init__`函数中创建一个`Entry`对象,并保存到`self.entry`变量中。
  1. self.entry = tk.Entry(self.root)
  2. self.entry.pack()
复制代码

2. 为输入框绑定回车事件`<Return>`,并指定回调函数`self.start_countdown`。
  1. self.entry.bind("<Return>", self.start_countdown)
复制代码

3. 实现回调函数`start_countdown`,在该函数中获取输入框中的数值,并将其转换为整型。
  1. def start_countdown(self, event):
  2.     try:
  3.         duration = int(self.entry.get())
  4.         # 创建一个新的倒计时器对象
  5.         countdown_timer = CountdownTimer(duration)
  6.     except ValueError:
  7.         # 处理非法输入的情况
  8.         print("请输入一个有效的数值")
复制代码

这样,当用户在输入框中输入数值后按下回车,就会创建一个新的倒计时器对象,从而开始倒计时。

最终的代码如下:
  1. import tkinter as tk
  2. class CountdownTimer:
  3.     def __init__(self, duration):
  4.         self.duration = duration
  5.         self.root = tk.Tk()
  6.         self.root.overrideredirect(1)  # 隐藏标题栏
  7.         self.root.attributes("-alpha", 0.8)  # 设置透明度
  8.         self.root.attributes("-topmost", True)  # 窗口置顶
  9.         # 获取屏幕宽高
  10.         screen_width = self.root.winfo_screenwidth()
  11.         screen_height = self.root.winfo_screenheight()
  12.         # 计算窗口位置
  13.         x = int(screen_width / 2)
  14.         y = int(screen_height / 2)
  15.         # 创建标签并添加到窗口
  16.         self.label = tk.Label(self.root, text=self.get_time(), font=("Arial", 36))
  17.         self.label.pack()
  18.         # 设置窗口位置
  19.         self.root.geometry("+{}+{}".format(x, y))
  20.         # 更新倒计时
  21.         self.update_timer()
  22.         # 创建输入框并添加到窗口
  23.         self.entry = tk.Entry(self.root)
  24.         self.entry.pack()
  25.         # 绑定回车事件
  26.         self.entry.bind("<Return>", self.start_countdown)
  27.         # 绑定鼠标事件
  28.         self.drag_start_x = 0
  29.         self.drag_start_y = 0
  30.         self.root.bind("<ButtonPress-1>", self.on_drag_start)
  31.         self.root.bind("<B1-Motion>", self.on_drag_motion)
  32.     def update_timer(self):
  33.         if self.duration > 0:
  34.             self.duration -= 1
  35.             self.label.config(text=self.get_time())
  36.             self.root.after(1000, self.update_timer)
  37.         else:
  38.             self.root.destroy()
  39.     def get_time(self):
  40.         minutes = self.duration // 60
  41.         seconds = self.duration % 60
  42.         return "{:02d}:{:02d}".format(minutes, seconds)
  43.     def on_drag_start(self, event):
  44.         self.drag_start_x = event.x
  45.         self.drag_start_y = event.y
  46.     def on_drag_motion(self, event):
  47.         x = self.root.winfo_x() + (event.x - self.drag_start_x)
  48.         y = self.root.winfo_y() + (event.y - self.drag_start_y)
  49.         self.root.geometry("+{}+{}".format(x, y))
  50.     def start_countdown(self, event):
  51.         try:
  52.             duration = int(self.entry.get())
  53.             # 创建一个新的倒计时器对象
  54.             countdown_timer = CountdownTimer(duration)
  55.         except ValueError:
  56.             # 处理非法输入的情况
  57.             print("请输入一个有效的数值")
  58. countdown_timer = CountdownTimer(60)
  59. countdown_timer.root.mainloop()
复制代码


以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-24 04:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表