按下tkinter按钮时如何播放声音?
问题描述:
我正在为Windows PC构建一个程序,该程序包含很多按钮,而且看起来很普通.所以我想知道,是否可以做到这一点,所以当您按一下按钮(使用tkinter)时,可以播放声音以使程序更生动一点吗?请记住,我正在学习,所以请稍作调整.
I am building a program for Windows PCs that contains a lot of buttons and seems very plain. So I was wondering, can I make it so when you push a button (using tkinter), can I play a sound to liven up the program a bit? Please keep in mind I am learning so please dumb it down a bit.
答
假设您的文件是WAV:
Assuming your file is a WAV:
from tkinter import *
from winsound import *
root = Tk() # create tkinter window
play = lambda: PlaySound('Sound.wav', SND_FILENAME)
button = Button(root, text = 'Play', command = play)
button.pack()
root.mainloop()
假设您的文件是MP3:
Assuming your file is a MP3:
from Tkinter import *
import mp3play
root = Tk() # create tkinter window
f = mp3play.load('Sound.mp3'); play = lambda: f.play()
button = Button(root, text = 'Play', command = play)
button.pack()
root.mainloop()