0

In python 2.7 I created a program with the Tkinter and Winsound modules, that is basically a simple virtual piano. What I want to do is have the note play while the button in the GUI is held down, and stop when the button is releasesd. Right now, it plays the sound after the button is released and for a specified time. Im sure this is simple but im new to programming.

import winsound
from Tkinter import *


global x
x = 250



def B():
    winsound.Beep(494, x)  #Frequency, Time in MS

def C():
    winsound.Beep(523, x)

def A():
    winsound.Beep(440, x)

def G():
    winsound.Beep(784, x)

def F():
    winsound.Beep(698, x)

def D():
    winsound.Beep(587, x)

def E():
    winsound.Beep(659, x)


root = Tk()
root.title('Piano')
root.geometry('600x600')


button1 = Button(root, text = 'A', command = A, height = '5', width = '20', fg = 'red').pack()
button2 = Button(root, text = 'B', command = B, height = '5', width = '20', fg = 'red').pack()
button3 = Button(root, text = 'C', command = C, height = '5', width = '20', fg = 'red').pack()
button4 = Button(root, text = 'D', command = D, height = '5', width = '20', fg = 'red').pack()
button5 = Button(root, text = 'E', command = E, height = '5', width = '20', fg = 'red').pack()
button6 = Button(root, text = 'F', command = F, height = '5', width = '20', fg = 'red').pack()


root.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
  • is there a way to tell winsound to start playing a sound (when button is pressed down) and then signal it to stop it playing (when released)? I really don't think "repeatedly play the sound" is what you want for a piano... – Tadhg McDonald-Jensen Oct 27 '16 at 21:06
  • As for handling clicking down and releasing you can't use button's `command` for this you would need to bind a `` event and `` for the simplest case but then if you click down on one button and drag across to another it would break so I'm not sure there is a fool proof way of accomplishing this. – Tadhg McDonald-Jensen Oct 27 '16 at 21:08

0 Answers0