我正在开发一个简单的计时器程序。时间到了,我会出现一个警告窗口,我也想用它播放一首歌。根据我使用 pygame.mixer 收集到的信息,它应该可以工作,但我的音频没有播放。这是我的代码:
from Tkinter import *
import tkMessageBox
import pygame
pygame.init()
pygame.mixer.init()
Sound = pygame.mixer.Sound("alarm.mp3")
def messageWindow():
win = Toplevel()
b = Button(win, text='Times Up!',
bg="yellow", fg="green",
activebackground="purple", activeforeground="white",
command=quit)
b.pack(ipadx=root.winfo_screenwidth()/2,
ipady=root.winfo_screenheight()/2)
root.mainloop()
def alert():
Sound.play()
messageWindow()
quit()
def start():
root.after(scale.get() * 1000, alert)
root = Tk()
minutes = Label(root, text ="Minutes: ")
minutes.grid(row=0, column=0)
scale = Scale(root, from_=1, to=60, orient=HORIZONTAL, length=450)
scale.grid(row=0, column=1)
button = Button(root,text= "Start Timing", command=start)
button.grid(row=1, column=1, pady=5, sticky=E)
root.mainloop()