鱼C论坛

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

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

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

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

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

x
在现有功能上,增加开始和重置按钮。
取消回车开始倒计时的操作
当重置后,时间设置为00:00;点击开始后,获取输入框的时间开始进行倒计时。
可以反复使用

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)  # 窗口置顶
        # 获取屏幕宽高
        screen_width = self.root.winfo_screenwidth()
        screen_height = self.root.winfo_screenheight()
        # 创建输入框并添加到窗口
        self.entry = tk.Entry(self.root)
        self.entry.pack()
        # 绑定回车事件
        self.entry.bind("<Return>", self.start_countdown)
        # 计算窗口位置
        x = 1700
        y = 50
        # 创建标签并添加到窗口
        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.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.label.config(text=self.get_time())
            self.root.after(1000, self.update_timer)
        else:
            self.is_counting = False
            # 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):
        print("开始倒计时")
        try:
            duration = int(self.entry.get())
            print(duration)
            # 更新倒计时
            if not self.is_counting:
                self.duration = duration
                print('laile')
                self.is_counting = True
                self.update_timer()
        except ValueError:
            # 处理非法输入的情况
            print("请输入一个有效的数值")
countdown_timer = CountdownTimer()
countdown_timer.root.mainloop()
最佳答案
2023-12-14 22:28:23
优化后的代码如下:
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)  # 窗口置顶                screen_width = self.root.winfo_screenwidth()        screen_height = self.root.winfo_screenheight()        self.entry = tk.Entry(self.root)        self.entry.pack()        self.start_button = tk.Button(self.root, text="开始", command=self.start_countdown)        self.start_button.pack()        self.reset_button = tk.Button(self.root, text="重置", command=self.reset_countdown)        self.reset_button.pack()        x = 1700        y = 50        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.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()
在原有代码的基础上,添加了两个按钮:开始按钮和重置按钮。开始按钮的点击事件绑定到`start_countdown`方法,重置按钮的点击事件绑定到`reset_countdown`方法。

同时,取消了回车键开始倒计时的操作。

当点击重置按钮后,倒计时器将被重置为00:00,并且停止计时。而点击开始按钮后,将会获取输入框中的时间,如果是一个大于0的有效数值,就开始计时。每次点击开始按钮,都会重新获取输入框中的时间,并重新开始倒计时。

这样,你就可以在重置之后,反复使用倒计时器了。

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

使用道具 举报

发表于 2023-12-14 22:28:23 | 显示全部楼层    本楼为最佳答案   
优化后的代码如下:
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)  # 窗口置顶                screen_width = self.root.winfo_screenwidth()        screen_height = self.root.winfo_screenheight()        self.entry = tk.Entry(self.root)        self.entry.pack()        self.start_button = tk.Button(self.root, text="开始", command=self.start_countdown)        self.start_button.pack()        self.reset_button = tk.Button(self.root, text="重置", command=self.reset_countdown)        self.reset_button.pack()        x = 1700        y = 50        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.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()
在原有代码的基础上,添加了两个按钮:开始按钮和重置按钮。开始按钮的点击事件绑定到`start_countdown`方法,重置按钮的点击事件绑定到`reset_countdown`方法。

同时,取消了回车键开始倒计时的操作。

当点击重置按钮后,倒计时器将被重置为00:00,并且停止计时。而点击开始按钮后,将会获取输入框中的时间,如果是一个大于0的有效数值,就开始计时。每次点击开始按钮,都会重新获取输入框中的时间,并重新开始倒计时。

这样,你就可以在重置之后,反复使用倒计时器了。

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-6-15 07:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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