鱼C论坛

 找回密码
 立即注册
查看: 1623|回复: 5

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

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

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

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

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()
最佳答案
2023-12-14 22:53:21
好的,根据您的要求,我已经修改了代码,使用文本框代替了label作为显示倒计时的控件。现在您可以直接运行这段代码来看看效果。
  1. import tkinter as tk
  2. class CountdownTimer:
  3.     def __init__(self):
  4.         self.is_counting = False
  5.         self.duration = 0
  6.         self.root = tk.Tk()
  7.         self.root.overrideredirect(1)  # 隐藏标题栏
  8.         self.root.attributes("-alpha", 0.2)  # 设置透明度
  9.         self.root.attributes("-topmost", True)  # 窗口置顶
  10.         self.textbox = tk.Entry(self.root, width=8, font=("Arial", 36))
  11.         self.textbox.pack(side=tk.TOP)
  12.         self.reset_button = tk.Button(self.root, text="reset", bg='red', command=self.reset_countdown)
  13.         self.reset_button.pack(side=tk.RIGHT)
  14.         self.start_button = tk.Button(self.root, text="start", bg='green',command=self.start_countdown)
  15.         self.start_button.pack(side=tk.RIGHT)
  16.         self.entry = tk.Entry(self.root, width=8, font=("Arial", 16))
  17.         self.entry.pack(side=tk.RIGHT)
  18.         x = 1600
  19.         y = 50
  20.         self.root.geometry("+{}+{}".format(x, y))
  21.         self.update_timer()
  22.         self.drag_start_x = 0
  23.         self.drag_start_y = 0
  24.         self.root.bind("<ButtonPress-1>", self.on_drag_start)
  25.         self.root.bind("<B1-Motion>", self.on_drag_motion)
  26.     def update_timer(self):
  27.         if self.duration > 0 and self.is_counting:
  28.             self.duration -= 1
  29.             self.textbox.delete(0, tk.END)      # 清空文本框内容
  30.             self.textbox.insert(tk.END, self.get_time())   # 在文本框插入新的倒计时时间
  31.             self.root.after(1000, self.update_timer)
  32.         else:
  33.             self.is_counting = False
  34.     def get_time(self):
  35.         minutes = self.duration // 60
  36.         seconds = self.duration % 60
  37.         return "{:02d}:{:02d}".format(minutes, seconds)
  38.     def on_drag_start(self, event):
  39.         self.drag_start_x = event.x
  40.         self.drag_start_y = event.y
  41.     def on_drag_motion(self, event):
  42.         x = self.root.winfo_x() + (event.x - self.drag_start_x)
  43.         y = self.root.winfo_y() + (event.y - self.drag_start_y)
  44.         self.root.geometry("+{}+{}".format(x, y))
  45.     def start_countdown(self):
  46.         if not self.is_counting:
  47.             try:
  48.                 duration = int(self.entry.get())
  49.                 if duration > 0:
  50.                     self.duration = duration
  51.                     self.is_counting = True
  52.                     self.update_timer()
  53.                 else:
  54.                     print("请输入一个正整数")
  55.             except ValueError:
  56.                 print("请输入一个有效的数值")
  57.     def reset_countdown(self):
  58.         self.is_counting = False
  59.         self.duration = 0
  60.         self.textbox.delete(0, tk.END)
  61.         self.textbox.insert(tk.END, self.get_time())
  62. countdown_timer = CountdownTimer()
  63. countdown_timer.root.mainloop()
复制代码

希望这次修改符合您的要求,如果还有其他问题,请随时告诉我。祝您编程愉快!

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

本帖被以下淘专辑推荐:

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

使用道具 举报

发表于 2023-12-14 22:53:21 | 显示全部楼层    本楼为最佳答案   
好的,根据您的要求,我已经修改了代码,使用文本框代替了label作为显示倒计时的控件。现在您可以直接运行这段代码来看看效果。
  1. import tkinter as tk
  2. class CountdownTimer:
  3.     def __init__(self):
  4.         self.is_counting = False
  5.         self.duration = 0
  6.         self.root = tk.Tk()
  7.         self.root.overrideredirect(1)  # 隐藏标题栏
  8.         self.root.attributes("-alpha", 0.2)  # 设置透明度
  9.         self.root.attributes("-topmost", True)  # 窗口置顶
  10.         self.textbox = tk.Entry(self.root, width=8, font=("Arial", 36))
  11.         self.textbox.pack(side=tk.TOP)
  12.         self.reset_button = tk.Button(self.root, text="reset", bg='red', command=self.reset_countdown)
  13.         self.reset_button.pack(side=tk.RIGHT)
  14.         self.start_button = tk.Button(self.root, text="start", bg='green',command=self.start_countdown)
  15.         self.start_button.pack(side=tk.RIGHT)
  16.         self.entry = tk.Entry(self.root, width=8, font=("Arial", 16))
  17.         self.entry.pack(side=tk.RIGHT)
  18.         x = 1600
  19.         y = 50
  20.         self.root.geometry("+{}+{}".format(x, y))
  21.         self.update_timer()
  22.         self.drag_start_x = 0
  23.         self.drag_start_y = 0
  24.         self.root.bind("<ButtonPress-1>", self.on_drag_start)
  25.         self.root.bind("<B1-Motion>", self.on_drag_motion)
  26.     def update_timer(self):
  27.         if self.duration > 0 and self.is_counting:
  28.             self.duration -= 1
  29.             self.textbox.delete(0, tk.END)      # 清空文本框内容
  30.             self.textbox.insert(tk.END, self.get_time())   # 在文本框插入新的倒计时时间
  31.             self.root.after(1000, self.update_timer)
  32.         else:
  33.             self.is_counting = False
  34.     def get_time(self):
  35.         minutes = self.duration // 60
  36.         seconds = self.duration % 60
  37.         return "{:02d}:{:02d}".format(minutes, seconds)
  38.     def on_drag_start(self, event):
  39.         self.drag_start_x = event.x
  40.         self.drag_start_y = event.y
  41.     def on_drag_motion(self, event):
  42.         x = self.root.winfo_x() + (event.x - self.drag_start_x)
  43.         y = self.root.winfo_y() + (event.y - self.drag_start_y)
  44.         self.root.geometry("+{}+{}".format(x, y))
  45.     def start_countdown(self):
  46.         if not self.is_counting:
  47.             try:
  48.                 duration = int(self.entry.get())
  49.                 if duration > 0:
  50.                     self.duration = duration
  51.                     self.is_counting = True
  52.                     self.update_timer()
  53.                 else:
  54.                     print("请输入一个正整数")
  55.             except ValueError:
  56.                 print("请输入一个有效的数值")
  57.     def reset_countdown(self):
  58.         self.is_counting = False
  59.         self.duration = 0
  60.         self.textbox.delete(0, tk.END)
  61.         self.textbox.insert(tk.END, self.get_time())
  62. countdown_timer = CountdownTimer()
  63. countdown_timer.root.mainloop()
复制代码

希望这次修改符合您的要求,如果还有其他问题,请随时告诉我。祝您编程愉快!

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

使用道具 举报

 楼主| 发表于 2023-12-14 23:09:14 | 显示全部楼层
  1. import tkinter as tk
  2. class CountdownTimer:
  3.     def __init__(self):
  4.         self.is_counting = False
  5.         self.duration = 0
  6.         self.root = tk.Tk()
  7.         self.root.overrideredirect(1)  # 隐藏标题栏
  8.         self.root.attributes("-alpha", 0.5)  # 设置透明度
  9.         self.root.attributes("-topmost", True)  # 窗口置顶
  10.         self.entry = tk.Entry(self.root, width=5, font=("Arial", 36, "bold"),fg='red', justify=tk.CENTER)
  11.         self.entry.pack(side=tk.LEFT)
  12.         self.reset_button = tk.Button(self.root, text="reset",height=2, command=self.reset_countdown)
  13.         self.reset_button.pack(side=tk.RIGHT)
  14.         self.start_button = tk.Button(self.root, text="start",height=2,command=self.start_countdown)
  15.         self.start_button.pack(side=tk.RIGHT)
  16.         x = 1600
  17.         y = 50
  18.         self.root.geometry("+{}+{}".format(x, y))
  19.         self.update_timer()
  20.         self.drag_start_x = 0
  21.         self.drag_start_y = 0
  22.         self.root.bind("<ButtonPress-1>", self.on_drag_start)
  23.         self.root.bind("<B1-Motion>", self.on_drag_motion)
  24.     def update_timer(self):
  25.         if self.duration > 0 and self.is_counting:
  26.             self.duration -= 1
  27.             self.entry.delete(0, tk.END)      # 清空文本框内容
  28.             self.entry.insert(tk.END, self.get_time())   # 在文本框插入新的倒计时时间
  29.             self.root.after(1000, self.update_timer)
  30.         else:
  31.             self.is_counting = False
  32.     def get_time(self):
  33.         minutes = self.duration // 60
  34.         seconds = self.duration % 60
  35.         return "{:02d}:{:02d}".format(minutes, seconds)
  36.     def on_drag_start(self, event):
  37.         self.drag_start_x = event.x
  38.         self.drag_start_y = event.y
  39.     def on_drag_motion(self, event):
  40.         x = self.root.winfo_x() + (event.x - self.drag_start_x)
  41.         y = self.root.winfo_y() + (event.y - self.drag_start_y)
  42.         self.root.geometry("+{}+{}".format(x, y))
  43.     def start_countdown(self):
  44.         if not self.is_counting:
  45.             try:
  46.                 duration = int(self.entry.get())
  47.                 if duration > 0:
  48.                     self.duration = duration
  49.                     self.is_counting = True
  50.                     self.update_timer()
  51.                 else:
  52.                     print("请输入一个正整数")
  53.             except ValueError:
  54.                 print("请输入一个有效的数值")
  55.     def reset_countdown(self):
  56.         self.is_counting = False
  57.         self.duration = 0
  58.         self.entry.delete(0, tk.END)
  59.         self.entry.insert(tk.END, self.get_time())
  60. countdown_timer = CountdownTimer()
  61. countdown_timer.root.mainloop()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-14 23:09:24 | 显示全部楼层
rsj0315 发表于 2023-12-14 23:09
import tkinter as tk
class CountdownTimer:
    d ...

非常棒!您的代码已经进行了一些优化,界面看起来更加美观。现在您可以尝试运行这段代码,并使用新的界面来进行倒计时操作了。希望这次修改符合您的要求,如果还有其他问题,请随时告诉我。祝您编程愉快!

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

使用道具 举报

 楼主| 发表于 2023-12-14 23:10:00 | 显示全部楼层

基本满意,可以打包了。
回头演讲ppt的时候可以用一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-12-14 23:10:26 | 显示全部楼层
rsj0315 发表于 2023-12-14 23:10
基本满意,可以打包了。
回头演讲ppt的时候可以用一 ...

很高兴您对代码的修改基本满意!如果您希望将代码打包为可执行文件,以便在演讲时更方便地展示,可以使用第三方库pyinstaller来完成打包操作。

首先,确保您已经安装了pyinstaller库。如果没有安装,可以使用以下命令进行安装:
  1. pip install pyinstaller
复制代码

安装完成后,进入代码所在的目录,在命令行中运行以下命令即可将代码打包为可执行文件:
  1. pyinstaller -w -F your_code.py
复制代码

其中,your_code.py是您保存代码的文件名。

运行完毕后,您会在同级目录下看到生成的dist文件夹,里面包含了可执行文件。您可以将此文件夹复制到其他任何计算机上运行,无需安装Python和所使用的库。

希望这个方法能帮助您将代码打包成可执行文件,并在演讲时顺利展示。如果还有其他问题,欢迎随时向我提问。祝您演讲成功!

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

使用道具 举报

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

本版积分规则

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

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

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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