Windows桌面番茄钟
介绍:
一直想找一个简简单单的番茄钟,就最基本的设置倒计时,固定最前面,占内存小。但是找不到哇,那就自己写一个简单的吧,效果还不错。
功能:
1. 始终在最前面
2. 可以拖动位置
3. 可以设置倒计时
3. 极度简洁
图片展示:
代码:
import os
import argparse
import tkinter as tk
from tkinter import messagebox, font
from tkinter import ttk
class PomodoroTimer:
def __init__(self, root, countdown_time):
self.root = root
self.root.title("") # 设置为空标题
self.root.geometry("150x100") # 缩小窗口
self.root.configure(bg="#FFCCCC")
self.root.attributes('-topmost', True)
# 获取当前文件目录并设置图标
current_dir = os.path.dirname(os.path.abspath(__file__))
icon_path = os.path.join(current_dir, 'tomato.ico')
self.root.iconbitmap(icon_path)
self.root.overrideredirect(True) # 隐藏标题栏
self.time_left = countdown_time # 设置倒计时
self.running = False
# 设置字体
self.custom_font = font.Font(family="Impact", size=24, weight="bold") # 设置字体大小
self.label_time = tk.Label(root, text=self.format_time(self.time_left), font=self.custom_font, bg="#FFCCCC", fg="#D24D57")
self.label_time.pack(pady=5) # 缩小间隔
# 创建样式
style = ttk.Style()
style.configure("Rounded.TButton", borderwidth=5, relief="raised", padding=5,
background="#FAEBD7", foreground="black", font=('Impact', 13)) # 设置按钮字体为加粗
style.map("Rounded.TButton",
background=[("active", "#FF6347"), ("pressed", "#FF6347")],
foreground=[("pressed", "white"), ("active", "black")])
# 创建开始/暂停按钮
self.toggle_button = ttk.Button(root, text="START!", command=self.toggle_timer, style="Rounded.TButton")
self.toggle_button.pack(pady=5) # 缩小间隔
# 将窗口放置在屏幕右上角
self.position_window()
self.update_clock()
# 添加拖动窗口功能
self.root.bind("<ButtonPress-1>", self.start_drag)
self.root.bind("<B1-Motion>", self.do_drag)
def format_time(self, seconds):
minutes = seconds // 60
seconds = seconds % 60
return f"{minutes:02}:{seconds:02}"
def update_clock(self):
if self.running and self.time_left > 0:
self.time_left -= 1
self.label_time.config(text=self.format_time(self.time_left))
elif self.time_left == 0:
self.running = False
messagebox.showinfo("Notification", "Time's up!")
return
self.root.after(1000, self.update_clock)
def toggle_timer(self):
if self.running:
self.running = False # 暂停
self.toggle_button.config(text="RESUME!") # 更新按钮文本
else:
self.running = True # 继续计时
self.toggle_button.config(text="PAUSE!") # 更新按钮文本
def on_closing(self):
self.root.destroy()
def position_window(self):
# 获取屏幕宽高
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
# 计算窗口的位置
x = screen_width - 150 # 窗口宽度
y = 0 # 顶部位置
self.root.geometry(f"150x100+{x}+{y}") # 设置窗口位置
def start_drag(self, event):
self.offset_x = event.x
self.offset_y = event.y
def do_drag(self, event):
x = self.root.winfo_x() - self.offset_x + event.x
y = self.root.winfo_y() - self.offset_y + event.y
self.root.geometry(f"+{x}+{y}")
def main():
# 设置命令行参数解析
parser = argparse.ArgumentParser(description="Pomodoro Timer")
parser.add_argument("--time", type=int, default=25, help="Countdown time in minutes (default: 25 minutes)")
args = parser.parse_args()
# 将分钟转换为秒
countdown_time = args.time * 60
root = tk.Tk()
timer = PomodoroTimer(root, countdown_time) # 传入倒计时参数
root.mainloop()
if __name__ == "__main__":
main()
PS: exe也有可以发,我一般用bat启动