tkinter中的按钮绑定

时间:2017-03-26 18:55:23

标签: python-3.x tkinter binding

我在Windows 7平台上使用Python 3.5,Tkinter 8.6。 对于下面的简单代码,我不断收到以下错误消息...

button2.bind("<Button-1>",PrintAddress)
AttributeError: 'NoneType' object has no attribute 'bind'

CODE

from tkinter import *

root = Tk()
root.geometry('200x200')

def PrintName():
    print("My name is ..........")

def PrintAddress(event):
    print("W223 N2257...........")


button1 = Button(root,text = 'Print Name', command=PrintName).grid(row = 0)    
button2 = Button(root,text = 'Print Address').grid(row = 0,column = 2) 
button2.bind("<Button-1>",PrintAddress)    

root.mainloop()

1 个答案:

答案 0 :(得分:2)

而不是:

filter

将其替换为

button2 = Button(root,text = 'Print Address').grid(row = 0, column = 2) 

Entry对象(以及所有其他小部件)的网格(和包和地点)功能返回None。在python中,当你执行a()。b()时,表达式的结果是b()返回的内容,因此Entry(...)。grid(...)将返回None -Nick