This little snippet combines a Tkinter GUI application with sound from the pygame mixer. The snippet should do fairly well across platforms. I tested it on a Windows XP machine, but let me know your experience with other platforms.
Tkinter Application with PyGame Sound
# play a sound on a key press, combines Tkinter with the PyGame mixer
# PyGame is free from: http://www.pygame.org/news.html
# tested with Python24 and PyGame171 vegaseat 15nov2006
from Tkinter import *
from pygame import mixer
def on_key(event):
# any normal key (0..9, A..Z, a..z)
if event.char == event.keysym:
if event.char == '1':
sound1.play()
if event.char == '2':
sound2.play()
prompt = 'Press key 1 or 2 to play a sound'
mixer.init(44100)
# pick sounds you have ...
# copy to working directory or indicate full path
try:
# create the sound instances
sound1 = mixer.Sound("boing.wav")
sound2 = mixer.Sound("beep.wav")
except:
prompt = "Error: Sound file not found"
root = Tk()
label1 = Label(root, text=prompt, width=len(prompt), bg='yellow', fg='red')
label1.pack()
label1.bind_all('<Key>', on_key)
root.mainloop()
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.