pycharm可以运行无报错,运行开始后无提示音播放

import schedule
import time
import datetime
import winsound
import pyttsx3
import sys
import os
import tkinter as tk


def hide_console():
    # 隐藏命令行窗口
    if sys.platform.startswith('win32'):
        import ctypes
        ctypes.windll.user32.ShowWindow(ctypes.windll.kernel32.GetConsoleWindow(), 0)
    elif sys.platform.startswith('linux'):
        os.system("xdotool search --onlyvisible --class 'Console' windowunmap")


def text_to_speech(text):
    engine = pyttsx3.init()
    engine.say(text)
    engine.runAndWait()

text = "请开始。"

def play_sound():
    for _ in range(1):
        winsound.PlaySound("sound.wav", winsound.SND_FILENAME)

def job():
    play_sound()
    time.sleep(0.5)
    text_to_speech(text)
    time.sleep(0.5)
    text_to_speech(text)
    current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    with open("log.txt", "a") as file:
        file.write("提示时间: " + current_time + "\n")

def create_button(root, text, command, width=None, height=None, padx=10, pady=10, expand=True):
    button = tk.Button(root, text=text, command=command, width=width, height=height)
    button.pack(side=tk.LEFT, padx=padx, pady=pady, expand=expand)
    return button

def start_job():
    global job_started
    if not job_started:
        for t in passtimes:
            schedule.every().day.at(t).do(job)
        job_started = True

def stop_job():
    global job_started
    if job_started:
        schedule.clear()
        job_started = False

# 创建窗口
root = tk.Tk()
root.title("巡视提示器")
root.geometry("300x100")

# 创建开始按钮
start_button = create_button(root, text="开始", command=start_job, width=10, height=2)

# 创建结束按钮
stop_button = create_button(root, text="结束", command=stop_job, width=10, height=2)

# 执行需要做的任务
passtimes = ["00:00","13:40", "14:00", "14:20",  "14:40", "20:00",  "21:00" , "22:40",  "23:30", "23:45"]

job_started = False

hide_console()

root.mainloop()

pycharm可以正常运行,弹出窗口点击开始,到达passtimes时间后无提示音。帮忙看一下那里出错。

winsound.PlaySound("sound.wav", winsound.SND_FILENAME)
单独运行这一行有播放么?这个文件存在么?用绝对路径看看

【以下回答由 GPT 生成】

问题分析: 根据代码的分析,问题可能出在以下几个方面: 1. 提示音文件路径是否正确。 2. 所用的提示音文件格式是否被支持。 3. 系统的默认音频输出设备是否正确设置。 4. 代码中音频播放的相关函数是否被正确调用。

解决方案: 根据以上问题分析,可以尝试以下解决方案来解决问题:

  1. 检查提示音文件路径是否正确:
  2. 确保sound.wav文件与代码所在的路径一致,或者修改代码中winsound.PlaySound的参数为正确的文件路径。

  3. 检查提示音文件格式是否被支持:

  4. 确保sound.wav文件的格式是被winsound库所支持的格式,如果不是,可以尝试将提示音文件转换为支持的格式,如.wav等。

  5. 检查系统的默认音频输出设备是否正确设置:

  6. 确保系统的默认音频输出设备已正确设置,确保没有将音频输出设备静音或调低音量。

  7. 确保代码中音频播放的相关函数被正确调用:

  8. 检查代码中音频播放的相关函数是否被正确调用,尤其是play_sound函数,确保该函数在需要播放音频时被正确调用。

如果尝试以上解决方案后仍未解决问题,那可能是其他问题导致的,可能需要更深入地检查硬件设备、操作系统以及相关库的设置。


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^