0

I am using Python 3.5, Tkinter 8.6 on a Windows 7 platform. For the simple code below, I keep getting the following error message...

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()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
ron
  • 19
  • 2
  • I guess this has been asked before...The problem is that you create the Button and directly call the grid function on it. grid returns None and this is assigned to your variable. Split the commands. – mkiever Mar 26 '17 at 18:58
  • Link to duplicate: http://stackoverflow.com/questions/13002843/attributeerror-nonetype-object-has-no-attribute-delete – mkiever Mar 26 '17 at 19:10
  • 1
    Possible duplicate of [Tkinter: AttributeError: NoneType object has no attribute get](http://stackoverflow.com/questions/1101750/tkinter-attributeerror-nonetype-object-has-no-attribute-get) – Bryan Oakley Mar 26 '17 at 20:53

1 Answers1

2

Instead of doing:

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

Replace it with

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

The grid (and pack, and place) function of the Entry object (and of all other widgets) returns None. In python when you do a().b(), the result of the expression is whatever b() returns, therefore Entry(...).grid(...) will return None -Nick

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
XzibitGG
  • 330
  • 5
  • 18