我试着在按下文本小部件中的空格键的同时调用press_space(),但按摩错误说“无类型”'对象没有属性' bind'我使用python 2.7
import nltk
root=Tk()
txt=Tkinter.Text(root,font=('arial',30,'bold'),bd=5, bg="grey",foreground="red",width="31",height="5").grid(columnspan=50,rowspan=30, column=0)
txt.bind('<space>', press_space)
def press_space():
print ('Hello world')
答案 0 :(得分:0)
非常受欢迎的错误
var = Widget(...).grid()
它分配给var
- grid()
返回的None
值 - 所以稍后您会None.bind()
你必须分两行
var = Widget(...)
var.grid()
之后bind()
将正常工作。
BTW: bind()
执行您必须接收的参数event
的函数。
import tkinter as tk
# --- functions ---
def press_space(event):
#print(event)
#print( dir(event) )
print('Hello world')
# --- main ---
root = tk.Tk()
txt = tk.Text(root)
txt.grid()
txt.bind('<space>', press_space)
root.mainloop()