0

I'm trying to learn about python GUI's but I can't figure out how to bind stuff.

from tkinter import *
root = Tk()

def leftClick(event):
    print("Left click")

frame = Frame(root,width=300,height=250).pack()
frame.bind("<Button-1>", leftClick)

root.mainloop()

but...

Traceback (most recent call last):
  File "gui2.py", line 8, in <module>
    frame.bind("<Button-1>", leftClick)
AttributeError: 'NoneType' object has no attribute 'bind'
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Cooper Hanson
  • 31
  • 1
  • 7

1 Answers1

1

To expand on eyllanesc's answer, the code should be

frame = Frame(root,width=300,height=250)
frame.pack()
frame.bind("<Button-1>", leftClick)

In general, methods (like pack) that modify the original object don't return anything. Your code created a Frame object, then called pack and stored the output in frame. This is equivalent to writing frame = None. You need to first store the object as frame and then modify it.

Also, a good package to start with for GUIs in Python is PySimpleGUI, if you're interested.

samm82
  • 240
  • 4
  • 14