Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I play a sound when a tkinter button is pushed?

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.

like image 287
Austin Hargis Avatar asked Mar 01 '15 16:03

Austin Hargis


1 Answers

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()

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()
like image 185
Malik Brahimi Avatar answered Oct 21 '22 02:10

Malik Brahimi