请使用python写一个悬浮窗口倒计时工具,要有以下功能 1、自定义倒计时时间 2、自定义倒计时结束时间提醒,并且语音提醒 3、有开始、暂停、重置、停止功能
时间: 2024-01-01 07:06:19 浏览: 258
可以使用Python的GUI库Tkinter来实现悬浮窗口倒计时工具。同时,你还需要使用Python的时间模块time来处理倒计时逻辑,以及使用Python的语音合成库pyttsx3来进行语音提醒。
下面是一个示例代码,实现了你所要求的功能:
```python
import tkinter as tk
import time
import pyttsx3
class CountdownTimer:
def __init__(self, root):
self.root = root
self.root.title("悬浮窗口倒计时工具")
self.countdown_sec = 0
self.remaining_sec = 0
self.timer_running = False
self.label_time = tk.Label(self.root, text="00:00:00", font=("Arial", 24))
self.label_time.pack(pady=20)
self.entry_time = tk.Entry(self.root)
self.entry_time.insert(0, "请输入倒计时时间(秒)")
self.entry_time.pack(pady=10)
self.button_start = tk.Button(self.root, text="开始", command=self.start_timer)
self.button_start.pack(pady=5)
self.button_pause = tk.Button(self.root, text="暂停", state=tk.DISABLED, command=self.pause_timer)
self.button_pause.pack(pady=5)
self.button_reset = tk.Button(self.root, text="重置", state=tk.DISABLED, command=self.reset_timer)
self.button_reset.pack(pady=5)
self.button_stop = tk.Button(self.root, text="停止", state=tk.DISABLED, command=self.stop_timer)
self.button_stop.pack(pady=5)
self.engine = pyttsx3.init()
def start_timer(self):
if self.timer_running:
return
try:
self.countdown_sec = int(self.entry_time.get())
if self.countdown_sec <= 0:
raise ValueError
except ValueError:
self.show_error_message("请输入一个正整数的倒计时时间")
return
self.remaining_sec = self.countdown_sec
self.update_time_label()
self.button_start.config(state=tk.DISABLED)
self.button_pause.config(state=tk.NORMAL)
self.button_reset.config(state=tk.NORMAL)
self.button_stop.config(state=tk.NORMAL)
self.timer_running = True
self.timer_tick()
def pause_timer(self):
if not self.timer_running:
return
self.timer_running = False
self.button_pause.config(text="继续", command=self.resume_timer)
def resume_timer(self):
if self.timer_running:
return
self.timer_running = True
self.button_pause.config(text="暂停", command=self.pause_timer)
self.timer_tick()
def reset_timer(self):
self.timer_running = False
self.remaining_sec = self.countdown_sec
self.update_time_label()
self.button_start.config(state=tk.NORMAL)
self.button_pause.config(state=tk.DISABLED, text="暂停", command=self.pause_timer)
self.button_reset.config(state=tk.DISABLED)
self.button_stop.config(state=tk.DISABLED)
def stop_timer(self):
self.timer_running = False
self.remaining_sec = 0
self.update_time_label()
self.button_start.config(state=tk.NORMAL)
self.button_pause.config(state=tk.DISABLED, text="暂停", command=self.pause_timer)
self.button_reset.config(state=tk.DISABLED)
self.button_stop.config(state=tk.DISABLED)
def timer_tick(self):
if self.remaining_sec <= 0:
self.timer_running = False
self.update_time_label()
self.play_audio()
return
self.update_time_label()
self.remaining_sec -= 1
if self.timer_running:
self.root.after(1000, self.timer_tick)
def update_time_label(self):
hours = self.remaining_sec // 3600
minutes = (self.remaining_sec % 3600) // 60
seconds = self.remaining_sec % 60
time_str = "{:02d}:{:02d}:{:02d}".format(hours, minutes, seconds)
self.label_time.config(text=time_str)
def play_audio(self):
self.engine.say("倒计时结束")
self.engine.runAndWait()
def show_error_message(self, message):
tk.messagebox.showerror("错误", message)
root = tk.Tk()
app = CountdownTimer(root)
root.mainloop()
```
运行以上代码,将会弹出一个悬浮窗口倒计时工具。你可以在输入框中输入倒计时时间(秒),然后点击开始按钮即可开始倒计时。在倒计时结束时,会有语音提醒。
请注意,你需要先安装pyttsx3库,可以使用以下命令安装:
```shell
pip install pyttsx3
```
希望以上代码能够满足你的需求!如有其他问题,请随时提问。
阅读全文
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)