I have a Python tkinter program simplified to
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, height=200, width=200, bg="salmon")
canvas.pack(fill=tk.BOTH, expand=tk.YES)
def click(event):
print(event.x)
print(event.y)
def release(event):
print(event.x)
print(event.y)
canvas.bind("<Button-1>", click)
canvas.bind("<ButtonRelease-1>", release)
root.mainloop()
with a Canvas as the main element. Canvas has click/release events bound to it (e.g. returning event.x
and event.y
). I want to add a background image to the canvas in this manner:
canvas = tk.Canvas(root, bg='/path/to/image.png')
I have managed to set a background image by creating an image on the canvas using canvas.create_image
method, as explained in Adding a background image in python. However, this broke my program as event.x
and event.y
return position of the background image.
I am looking for a solution that would force me to change the least of the existing code.