在 python 2.7 中,我使用 Tkinter 和 Winsound 模块创建了一个程序,它基本上是一个简单的虚拟钢琴。我想要做的是在按住 GUI 中的按钮时播放音符,并在释放按钮时停止。现在,它会在释放按钮后播放指定时间的声音。我确信这很简单,但我对编程很陌生。
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()