好友
阅读权限 10
听众
最后登录 1970-1-1
1 00:00:00 鼠标放到每个0上滚轮可以调数字
2 倒计时结束时 会有5声提示音
3 电脑休眠按钮和关机按钮 倒计时结束时 会有5声提示音 并进入10 9 8 7 。。10秒的最后执行 在这时点相应按钮会停止执行 就是取消
4 点运行程序 可以添加一个程序 倒计时结束时 会有5声提示音 并进入10 9 8 7 。。10秒的最后执行 在这时点相应按钮会停止执行 就是取消
5 设置时间 点相应功能 最后确认就行了。
蓝奏:https://wwo.lanzouq.com/i2otp24alpng
窗口尺寸300*300 窗口起始位 500*500
self.root.geometry("300x300+500+500")
import tkinter as tk
from tkinter import filedialog, messagebox
import os
import platform
import time
import winsound
class TimerApp:
def __init__(self, root):
self.root = root
self.root.title("定时程序")
self.root.geometry("300x300+500+500")
self.hours_tens = 0
self.hours_units = 0
self.minutes_tens = 0
self.minutes_units = 0
self.seconds_tens = 0
self.seconds_units = 0
self.time_var = tk.StringVar(value="00:00:00")
self.create_widgets()
self.timer_running = False
self.remaining_time = 0
self.sleep_triggered = False
self.run_program_path = ""
self.run_program_countdown_running = False
self.shutdown_triggered = False
self.flash_colors = ["red", "orange", "yellow", "green", "blue", "indigo", "violet"]
self.flash_index = 0
def create_widgets(self):
top_frame = tk.Frame(self.root)
top_frame.pack(pady=10)
self.time_display = tk.Label(top_frame, textvariable=self.time_var, font=("Helvetica", 24))
self.time_display.pack(side=tk.LEFT)
self.time_display.bind("<Enter>", self.bind_mouse_wheel)
self.time_display.bind("<Leave>", self.unbind_mouse_wheel)
reset_button = tk.Button(top_frame, text="重置", command=self.reset_time)
reset_button.pack(side=tk.LEFT, padx=5)
self.confirm_button = tk.Button(top_frame, text="确认", command=self.start_timer)
self.confirm_button.pack(side=tk.LEFT, padx=5)
time_adjust_frame_top = tk.Frame(self.root)
time_adjust_frame_top.pack(pady=5)
time_adjust_frame_bottom = tk.Frame(self.root)
time_adjust_frame_bottom.pack(pady=5)
time_adjust_buttons_top = [
("+30秒", 30),
("+1分", 60),
("+3分", 180),
("+5分", 300),
("+10分", 600),
("+15分", 900)
]
time_adjust_buttons_bottom = [
("+20分", 1200),
("+25分", 1500),
("+30分", 1800),
("+1小时", 3600),
("+2小时", 7200)
]
for text, delta in time_adjust_buttons_top:
btn = tk.Button(time_adjust_frame_top, text=text, command=lambda d=delta: self.adjust_time(d))
btn.pack(side=tk.LEFT, padx=2)
for text, delta in time_adjust_buttons_bottom:
btn = tk.Button(time_adjust_frame_bottom, text=text, command=lambda d=delta: self.adjust_time(d))
btn.pack(side=tk.LEFT, padx=2)
self.flash_button = tk.Button(self.root, text="倒计时结束", command=self.stop_flash)
self.flash_button.pack(pady=10)
self.flash_button.config(state=tk.DISABLED)
control_frame = tk.Frame(self.root)
control_frame.pack(pady=10)
self.sleep_button = tk.Button(control_frame, text="电脑休眠", command=self.toggle_sleep)
self.sleep_button.pack(side=tk.LEFT, padx=5)
self.sleep_button.config(state=tk.NORMAL)
self.run_program_button = tk.Button(control_frame, text="运行程序", command=self.toggle_run_program)
self.run_program_button.pack(side=tk.LEFT, padx=5)
self.run_program_button.config(state=tk.NORMAL)
self.shutdown_button = tk.Button(control_frame, text="关机", command=self.toggle_shutdown)
self.shutdown_button.pack(side=tk.LEFT, padx=5)
self.shutdown_button.config(state=tk.NORMAL)
def reset_time(self):
self.hours_tens = 0
self.hours_units = 0
self.minutes_tens = 0
self.minutes_units = 0
self.seconds_tens = 0
self.seconds_units = 0
self.update_time_var()
self.timer_running = False
self.confirm_button.config(state=tk.NORMAL)
self.flash_button.config(state=tk.DISABLED)
self.flash_button.flash = False
self.flash_button.config(bg=self.root.cget("bg"), text="倒计时结束")
self.sleep_button.config(state=tk.NORMAL, bg=self.root.cget("bg"), text="电脑休眠")
self.sleep_triggered = False
self.run_program_button.config(state=tk.NORMAL, bg=self.root.cget("bg"), text="运行程序")
self.run_program_countdown_running = False
self.run_program_path = ""
self.shutdown_button.config(state=tk.NORMAL, bg=self.root.cget("bg"), text="关机")
self.shutdown_triggered = False
if hasattr(self, 'timer_id'):
self.root.after_cancel(self.timer_id)
if hasattr(self, 'sleep_id'):
self.root.after_cancel(self.sleep_id)
if hasattr(self, 'run_program_id'):
self.root.after_cancel(self.run_program_id)
if hasattr(self, 'shutdown_id'):
self.root.after_cancel(self.shutdown_id)
def adjust_time(self, delta):
current_time = self.get_seconds()
new_time = current_time + delta
self.set_time(new_time)
def start_timer(self):
if self.timer_running:
return
self.remaining_time = self.get_seconds()
if self.remaining_time <= 0:
messagebox.showwarning("警告", "请设置一个有效的时间")
return
self.timer_running = True
self.confirm_button.config(state=tk.DISABLED)
self.countdown()
def countdown(self):
if self.remaining_time <= 0:
self.time_var.set("00:00:00")
self.play_alert_sounds()
self.flash_button.config(state=tk.NORMAL)
self.flash_button.flash = True
self.flash_button_flashing()
self.schedule_sleep()
self.schedule_run_program()
self.schedule_shutdown()
return
minutes, seconds = divmod(self.remaining_time, 60)
hours, minutes = divmod(minutes, 60)
self.time_var.set(f"{hours:02}:{minutes:02}:{seconds:02}")
self.remaining_time -= 1
self.timer_id = self.root.after(1000, self.countdown)
def stop_flash(self):
self.flash_button.flash = False
self.flash_button.config(state=tk.DISABLED)
self.confirm_button.config(state=tk.NORMAL)
def toggle_sleep(self):
if self.sleep_triggered:
self.reset_sleep_button()
else:
self.sleep_button.config(bg="red")
self.sleep_triggered = True
def schedule_sleep(self):
if self.sleep_triggered:
self.sleep_button.flash = True
self.sleep_button_flashing()
self.sleep_countdown = 10
self.sleep_countdown_display()
def sleep_countdown_display(self):
if self.sleep_countdown <= 0:
self.sleep_computer()
return
self.sleep_button.config(text=str(self.sleep_countdown))
self.sleep_countdown -= 1
self.sleep_id = self.root.after(1000, self.sleep_countdown_display)
def sleep_computer(self):
self.sleep_button.flash = False
self.sleep_button.config(state=tk.DISABLED, text="电脑休眠")
if platform.system() == "Windows":
os.system("rundll32.exe powrprof.dll,SetSuspendState 0,1,0")
elif platform.system() == "Darwin":
os.system("pmset sleepnow")
elif platform.system() == "Linux":
os.system("systemctl suspend")
def reset_sleep_button(self):
self.sleep_button.flash = False
self.sleep_button.config(bg=self.root.cget("bg"), text="电脑休眠")
self.sleep_triggered = False
if hasattr(self, 'sleep_id'):
self.root.after_cancel(self.sleep_id)
def toggle_run_program(self):
if self.run_program_countdown_running:
self.run_program_button.flash = False
self.run_program_button.config(text="运行程序", bg=self.root.cget("bg"))
self.run_program_countdown_running = False
self.root.after_cancel(self.run_program_id)
else:
self.run_program_path = filedialog.askopenfilename()
if self.run_program_path:
self.run_program_button.config(bg="red")
def schedule_run_program(self):
if self.run_program_path:
self.run_program_button.flash = True
self.run_program_button_flashing()
self.run_program_countdown = 10
self.run_program_countdown_display()
self.run_program_countdown_running = True
def run_program_countdown_display(self):
if self.run_program_countdown <= 0:
self.execute_run_program()
return
self.run_program_button.config(text=str(self.run_program_countdown))
self.run_program_countdown -= 1
self.run_program_id = self.root.after(1000, self.run_program_countdown_display)
def execute_run_program(self):
self.run_program_button.flash = False
self.run_program_button.config(state=tk.DISABLED, text="运行程序")
os.startfile(self.run_program_path)
def toggle_shutdown(self):
if self.shutdown_triggered:
self.reset_shutdown_button()
else:
self.shutdown_button.config(bg="red")
self.shutdown_triggered = True
def schedule_shutdown(self):
if self.shutdown_triggered:
self.shutdown_button.flash = True
self.shutdown_button_flashing()
self.shutdown_countdown = 10
self.shutdown_countdown_display()
def shutdown_countdown_display(self):
if self.shutdown_countdown <= 0:
self.shutdown_computer()
return
self.shutdown_button.config(text=str(self.shutdown_countdown))
self.shutdown_countdown -= 1
self.shutdown_id = self.root.after(1000, self.shutdown_countdown_display)
def shutdown_computer(self):
self.shutdown_button.flash = False
self.shutdown_button.config(state=tk.DISABLED, text="关机")
if platform.system() == "Windows":
os.system("shutdown /s /t 1")
elif platform.system() == "Darwin":
os.system("sudo shutdown -h now")
elif platform.system() == "Linux":
os.system("systemctl poweroff")
def reset_shutdown_button(self):
self.shutdown_button.flash = False
self.shutdown_button.config(bg=self.root.cget("bg"), text="关机")
self.shutdown_triggered = False
if hasattr(self, 'shutdown_id'):
self.root.after_cancel(self.shutdown_id)
def play_alert_sounds(self):
for _ in range(5):
winsound.Beep(1000, 500)
time.sleep(0.5)
def flash_button_flashing(self):
if not hasattr(self.flash_button, 'flash') or not self.flash_button.flash:
return
self.flash_button.config(bg=self.flash_colors[self.flash_index])
self.flash_index = (self.flash_index + 1) % len(self.flash_colors)
self.root.after(500, self.flash_button_flashing)
def sleep_button_flashing(self):
if not hasattr(self.sleep_button, 'flash') or not self.sleep_button.flash:
return
current_color = self.sleep_button.cget("bg")
next_color = "red" if current_color == "yellow" else "yellow"
self.sleep_button.config(bg=next_color)
self.root.after(500, self.sleep_button_flashing)
def run_program_button_flashing(self):
if not hasattr(self.run_program_button, 'flash') or not self.run_program_button.flash:
return
current_color = self.run_program_button.cget("bg")
next_color = "red" if current_color == "yellow" else "yellow"
self.run_program_button.config(bg=next_color)
self.root.after(500, self.run_program_button_flashing)
def shutdown_button_flashing(self):
if not hasattr(self.shutdown_button, 'flash') or not self.shutdown_button.flash:
return
current_color = self.shutdown_button.cget("bg")
next_color = "red" if current_color == "yellow" else "yellow"
self.shutdown_button.config(bg=next_color)
self.root.after(500, self.shutdown_button_flashing)
def get_seconds(self):
return (
self.hours_tens * 36000 +
self.hours_units * 3600 +
self.minutes_tens * 600 +
self.minutes_units * 60 +
self.seconds_tens * 10 +
self.seconds_units
)
def set_time(self, total_seconds):
minutes, seconds = divmod(total_seconds, 60)
hours, minutes = divmod(minutes, 60)
self.hours_tens, self.hours_units = divmod(hours, 10)
self.minutes_tens, self.minutes_units = divmod(minutes, 10)
self.seconds_tens, self.seconds_units = divmod(seconds, 10)
self.update_time_var()
def update_time_var(self):
self.time_var.set(f"{self.hours_tens}{self.hours_units}:"
f"{self.minutes_tens}{self.minutes_units}:"
f"{self.seconds_tens}{self.seconds_units}")
def bind_mouse_wheel(self, event):
self.time_display.bind("<MouseWheel>", self.on_mouse_wheel)
def unbind_mouse_wheel(self, event):
self.time_display.unbind("<MouseWheel>")
def on_mouse_wheel(self, event):
widget = event.widget
widget_x = widget.winfo_pointerx() - widget.winfo_rootx()
if widget_x < 24:
delta = event.delta // 120
self.hours_tens = (self.hours_tens + delta) % 10
elif widget_x < 48:
delta = event.delta // 120
self.hours_units = (self.hours_units + delta) % 10
elif widget_x < 72:
delta = event.delta // 120
self.minutes_tens = (self.minutes_tens + delta) % 6
elif widget_x < 96:
delta = event.delta // 120
self.minutes_units = (self.minutes_units + delta) % 10
elif widget_x < 120:
delta = event.delta // 120
self.seconds_tens = (self.seconds_tens + delta) % 6
elif widget_x < 144:
delta = event.delta // 120
self.seconds_units = (self.seconds_units + delta) % 10
self.update_time_var()
if __name__ == "__main__":
root = tk.Tk()
app = TimerApp(root)
root.mainloop()
免费评分
查看全部评分