python - 绑定(bind)按键事件中的错误消息

标签 python tkinter

我正在努力将左箭头按键与打印的消息绑定(bind)在一起。有什么想法吗?我搜索过这个网站,似乎遵循了其他人使用过的相同说明,但它仍然不起作用!我对 Python(以及一般编码)非常陌生,非常感谢您的帮助!

错误信息:

NameError: name 'onArrowKey' is not defined

相关代码:

from Tkinter import*

class FullScreenApp(object):
    def __init__(self, master):
        self.master=master
        pad=3
        self._geom='200x200+0+0'
        master.geometry("{0}x{1}+0+0".format(
        master.winfo_screenwidth()-pad, master.winfo_screenheight()-pad))
        master.bind('<Escape>',self.toggle_geom)
    def toggle_geom(self,event):
        geom=self.master.winfo_geometry()
        print(geom,self._geom)
        self.master.geometry(self._geom)
        self._geom=geom
    def onArrowKey(self,event): 
        print 'Got arrow key press'


root=Tk()
root.bind('<Left>', onArrowKey)
root.config(background="black")
root.title("Who buzzed in first?")
app=FullScreenApp(root)
root.mainloop()

最佳答案

在您正在编写的上下文中,onArrowKey 没有定义。将 def onArrowKey 移出 FullScreenApp 类,以便上下文正确,或者调用 app.onArrowKey 而不是 onArrowKey(分配应用程序后)。

关于python - 绑定(bind)按键事件中的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34796758/

相关文章:

python - 导入错误 : No module named _imagingtk

python - 如何制作等待用户响应的对话框?

python - 在 Python Tkinter 窗口中显示 Google Map API

python - 在 Django 管理更改/添加页面中显示外键数据

python属性样式

python - 隐藏所有这些网格,这样我就可以制作另一个 'page'

python - 为什么我得到 'module' object is not callable in python 3?

pycharm调试器中的python 3.4 AssertionError

python - 为什么 Python MySQLdb 中的 executemany 很慢?

python - 向 Python 中的所有线程发送单向消息