鱼C论坛

 找回密码
 立即注册
查看: 2116|回复: 2

[已解决]Python 写一个悬浮桌面上的倒计时器

[复制链接]
发表于 2023-12-13 23:30:23 From FishC Mobile | 显示全部楼层 |阅读模式

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

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

x
Python 写一个悬浮桌面上的倒计时器
要求:
1.半透明
2.悬浮
3.倒计时长可以设定
4.无边框
最佳答案
2023-12-13 23:31:03
这是一个有趣的项目!为了实现这个悬浮桌面上的倒计时器,我们可以使用Python中的Tkinter库和Win32gui库。

首先,你需要安装Win32gui库。可以使用以下命令通过pip进行安装:
  1. pip install pywin32
复制代码

然后,你可以尝试以下代码:
  1. import tkinter as tk
  2. import win32gui
  3. class CountdownTimer:
  4.     def __init__(self, duration):
  5.         self.duration = duration
  6.         self.root = tk.Tk()
  7.         self.root.overrideredirect(1)  # 隐藏标题栏
  8.         self.root.attributes("-alpha", 0.8)  # 设置透明度
  9.         self.root.attributes("-topmost", True)  # 窗口置顶
  10.         # 获取屏幕宽高
  11.         screen_width = self.root.winfo_screenwidth()
  12.         screen_height = self.root.winfo_screenheight()
  13.         # 计算窗口位置
  14.         x = int(screen_width / 2)
  15.         y = int(screen_height / 2)
  16.         # 创建标签并添加到窗口
  17.         self.label = tk.Label(self.root, text=self.get_time(), font=("Arial", 36))
  18.         self.label.pack()
  19.         # 设置窗口位置
  20.         self.root.geometry("+{}+{}".format(x, y))
  21.         # 更新倒计时
  22.         self.update_timer()
  23.     def update_timer(self):
  24.         if self.duration <= 0:
  25.             self.label.config(text="Time's up!")
  26.             self.root.after(2000, self.close_window)  # 显示 "Time's up!" 两秒后关闭窗口
  27.         else:
  28.             self.label.config(text=self.get_time())
  29.             self.duration -= 1
  30.             self.root.after(1000, self.update_timer)  # 每一秒更新一次倒计时
  31.     def get_time(self):
  32.         minutes = self.duration // 60
  33.         seconds = self.duration % 60
  34.         return "{:02d}:{:02d}".format(minutes, seconds)
  35.     def close_window(self):
  36.         self.root.destroy()
  37. def set_transparent(hwnd):
  38.     # 设置窗口透明度为20%
  39.     win32gui.SetWindowLong(hwnd, win32gui.GWL_EXSTYLE,
  40.                            win32gui.GetWindowLong(hwnd, win32gui.GWL_EXSTYLE) | win32gui.WS_EX_LAYERED)
  41.     win32gui.SetLayeredWindowAttributes(hwnd, win32gui.RGB(0, 0, 0), 0, win32gui.LWA_ALPHA)
  42. if __name__ == "__main__":
  43.     timer = CountdownTimer(180)  # 设置倒计时时长为3分钟
  44.     # 获取窗口句柄
  45.     hwnd = int(timer.root.winfo_id())
  46.     # 设置窗口半透明
  47.     set_transparent(hwnd)
  48.     # 进入事件循环
  49.     timer.root.mainloop()
复制代码

这段代码会创建一个指定时长的倒计时器窗口,并设定其半透明、悬浮、无边框等特性。你可以通过修改`CountdownTimer`类中的`duration`参数来设置倒计时时长。

希望这个例子能满足你的需求!如果有任何问题,请随时提问。

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

本帖被以下淘专辑推荐:

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

使用道具 举报

发表于 2023-12-13 23:31:03 | 显示全部楼层    本楼为最佳答案   
这是一个有趣的项目!为了实现这个悬浮桌面上的倒计时器,我们可以使用Python中的Tkinter库和Win32gui库。

首先,你需要安装Win32gui库。可以使用以下命令通过pip进行安装:
  1. pip install pywin32
复制代码

然后,你可以尝试以下代码:
  1. import tkinter as tk
  2. import win32gui
  3. class CountdownTimer:
  4.     def __init__(self, duration):
  5.         self.duration = duration
  6.         self.root = tk.Tk()
  7.         self.root.overrideredirect(1)  # 隐藏标题栏
  8.         self.root.attributes("-alpha", 0.8)  # 设置透明度
  9.         self.root.attributes("-topmost", True)  # 窗口置顶
  10.         # 获取屏幕宽高
  11.         screen_width = self.root.winfo_screenwidth()
  12.         screen_height = self.root.winfo_screenheight()
  13.         # 计算窗口位置
  14.         x = int(screen_width / 2)
  15.         y = int(screen_height / 2)
  16.         # 创建标签并添加到窗口
  17.         self.label = tk.Label(self.root, text=self.get_time(), font=("Arial", 36))
  18.         self.label.pack()
  19.         # 设置窗口位置
  20.         self.root.geometry("+{}+{}".format(x, y))
  21.         # 更新倒计时
  22.         self.update_timer()
  23.     def update_timer(self):
  24.         if self.duration <= 0:
  25.             self.label.config(text="Time's up!")
  26.             self.root.after(2000, self.close_window)  # 显示 "Time's up!" 两秒后关闭窗口
  27.         else:
  28.             self.label.config(text=self.get_time())
  29.             self.duration -= 1
  30.             self.root.after(1000, self.update_timer)  # 每一秒更新一次倒计时
  31.     def get_time(self):
  32.         minutes = self.duration // 60
  33.         seconds = self.duration % 60
  34.         return "{:02d}:{:02d}".format(minutes, seconds)
  35.     def close_window(self):
  36.         self.root.destroy()
  37. def set_transparent(hwnd):
  38.     # 设置窗口透明度为20%
  39.     win32gui.SetWindowLong(hwnd, win32gui.GWL_EXSTYLE,
  40.                            win32gui.GetWindowLong(hwnd, win32gui.GWL_EXSTYLE) | win32gui.WS_EX_LAYERED)
  41.     win32gui.SetLayeredWindowAttributes(hwnd, win32gui.RGB(0, 0, 0), 0, win32gui.LWA_ALPHA)
  42. if __name__ == "__main__":
  43.     timer = CountdownTimer(180)  # 设置倒计时时长为3分钟
  44.     # 获取窗口句柄
  45.     hwnd = int(timer.root.winfo_id())
  46.     # 设置窗口半透明
  47.     set_transparent(hwnd)
  48.     # 进入事件循环
  49.     timer.root.mainloop()
复制代码

这段代码会创建一个指定时长的倒计时器窗口,并设定其半透明、悬浮、无边框等特性。你可以通过修改`CountdownTimer`类中的`duration`参数来设置倒计时时长。

希望这个例子能满足你的需求!如果有任何问题,请随时提问。

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

使用道具 举报

发表于 2023-12-13 23:42:24 | 显示全部楼层
import tkinter as tk

def countdown(seconds):
    root = tk.Tk()
    root.overrideredirect(True)
    root.attributes("-alpha", 0.7)
    root.attributes("-topmost", True)
    root.attributes("-transparentcolor", "white")

    label = tk.Label(root, text="", font=("Arial", 30))
    label.pack()

    def update_label(secs):
        if secs > 0:
            label.config(text=str(secs))
            secs -= 1
            root.after(1000, update_label, secs)
        else:
            root.destroy()

    update_label(seconds)
    root.mainloop()

countdown(60)  # Set countdown time in seconds
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-26 07:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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