所以我正在研究节奏训练器,并使用 wxpython 作为 UI 工具包。我想知道是否有人知道如何绑定键盘按键来播放声音?所以简单地说,用户可以使用键盘来打鼓。示例“按 A 键将播放低音鼓”
现在我遇到了一个教程-
http://www.blog.pythonlibrary.org/2009/08/29/wxpython- catch-key-and-char-events/
但这似乎需要按钮才能成功播放声音。我有一些使用这个例子的功能。但我想知道是否有另一种不需要按钮的方式来做到这一点?
import wx
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Key Press Tutorial")
# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)
btn = wx.Button(panel, label="OK")
btn.Bind(wx.EVT_KEY_DOWN, self.onKeyPress)
def onKeyPress(self, event):
keycode = event.GetKeyCode()
print keycode
if keycode == ord('A'):
print "you pressed the spacebar!"
sound_file = "notation1.wav"
sound=wx.Sound(sound_file)
print(sound_file)
sound.Play(wx.SOUND_ASYNC)
event.Skip()
# Run the program
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = MyForm()
frame.Show()
app.MainLoop()
这是我使用教程的示例。
干杯!