light_y
light_y

Reputation: 15

Code not printing the events in tkinter python

So I'm trying to make a code that prints the events/actions that happens on the tkinter area. When I run the script I don't get an error but when I'm clicking on the graphics area nothing prints out.

import tkinter
canvas = tkinter.Canvas(width=640, height=480)
canvas.pack()
def function1(event):
    print(repr(event))
canvas.bind("ButtonPress-1", function1)
canvas.mainloop()

Upvotes: 1

Views: 191

Answers (1)

milanbalazs
milanbalazs

Reputation: 5339

You need to define the instance of tkinter.Tk() and use it as root. The following implementation works for me as expected:

import tkinter

root = tkinter.Tk()
def function1(event):
    print(repr(event))

canvas = tkinter.Canvas(root, width=640, height=480)
canvas.bind("<ButtonPress-1>", function1)
canvas.pack()
root.mainloop()

Upvotes: 1

Related Questions