鱼C论坛

 找回密码
 立即注册
查看: 560|回复: 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作为显示倒计时的控件。现在您可以直接运行这段代码来看看效果。
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 小助理,如未能正确解答您的问题,请继续追问。

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-14 22:53:21 | 显示全部楼层    本楼为最佳答案   
好的,根据您的要求,我已经修改了代码,使用文本框代替了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 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-12-14 23:09:14 | 显示全部楼层
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.5)  # 设置透明度
        self.root.attributes("-topmost", True)  # 窗口置顶
        self.entry = tk.Entry(self.root, width=5, font=("Arial", 36, "bold"),fg='red', justify=tk.CENTER)
        self.entry.pack(side=tk.LEFT)
        self.reset_button = tk.Button(self.root, text="reset",height=2, command=self.reset_countdown)
        self.reset_button.pack(side=tk.RIGHT)
        self.start_button = tk.Button(self.root, text="start",height=2,command=self.start_countdown)
        self.start_button.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.entry.delete(0, tk.END)      # 清空文本框内容
            self.entry.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.entry.delete(0, tk.END)
        self.entry.insert(tk.END, self.get_time())
countdown_timer = CountdownTimer()
countdown_timer.root.mainloop()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

基本满意,可以打包了。
回头演讲ppt的时候可以用一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

首先,确保您已经安装了pyinstaller库。如果没有安装,可以使用以下命令进行安装:
pip install pyinstaller
安装完成后,进入代码所在的目录,在命令行中运行以下命令即可将代码打包为可执行文件:
pyinstaller -w -F your_code.py
其中,your_code.py是您保存代码的文件名。

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

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

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 20:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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