How to Make a Beep Sound in Linux and a Fun Program that Uses it

I came across my first Python script using Tkinter tonight while looking through old files. It creates a big screen where you can click anywhere on it and it makes your computer beep. Your Y position controls the frequency and your X position controls the duration.

Well, I wanted to give it a run for old time’s sake, but alas:
ImportError: No module named winsound (On Linux)

So I looked up the problem and decided to go with the beep package installed by running:
sudo apt-get install beep

Then I updated my script to run on both Windows and Linux. Enjoy*:

#!usr/bin/env/python

from Tkinter import *

try:
    import winsound
except ImportError:
    import os
    def playsound(frequency,duration):
        #apt-get install beep
        os.system('beep -f %s -l %s' % (frequency,duration))
else:
    def playsound(frequency,duration):
        winsound.Beep(frequency,duration)

root = Tk()
def callback(event):
    print "clicked at", event.x, event.y
    frequency=event.y * 6
    duration=event.x/2
    print "Freq= ", frequency, "HZ"
    print "duration=",duration, "ms"
    print ""
    if (frequency < 32000) and (frequency>40):
        playsound(frequency,duration)

frame = Frame(root, width=1000, height=800)
frame.bind("<Button-1>", callback)
frame.pack()
#message=Label(frame)
root.mainloop()

* Or annoy those near you …

Comments are closed.