This simple demo显示了如何在PySimpleGUI中读取击键。但是,当我尝试运行PySimpleGUI Pong demo时,我的击键没有被视为事件。有人可以解释为什么吗?谢谢。
P.S。将use_default_focus=False
添加到PySimpleGUI Pong演示中无济于事。
更新:我找到了游戏的原始Tkinter版本。它包括
self.canvas.bind_all('w', self.up)
self.canvas.bind_all('s', self.down)
和
self.canvas.bind_all('w', self.up)
self.canvas.bind_all('s', self.down)
在两个__init__
类中pongbat
方法的末尾。 (此版本完全不使用PySimpleGUI。)通过此更改,PySimpleGUI版本也可以使用。 (但这不是必须的!应该有可能获得这些击键。)
结果证明该解决方案非常令人惊讶。原始的SimplePyGUI代码使用此行创建一个Window。
window = sg.Window('The Classic Game of Pong',
return_keyboard_events=True).Layout(layout).Finalize()
删除return_keyboard_events=True
使它起作用!