python - Can you set a tkinter background image without adding a new canvas element? -


i have python tkinter program simplified to

import tkinter 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 canvas main element. canvas has click/release events bound (e.g. returning event.x , event.y). want add background image canvas in manner:

canvas = tk.canvas(root, bg='/path/to/image.png') 

i have managed set background image creating image on canvas using canvas.create_image method, explained in adding background image in python. however, broke program event.x , event.y return position of background image.

i looking solution force me change least of existing code.

we need use photoimage load image use use create_image set image canvas.

give shot:

import tkinter 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)  my_image = tk.photoimage(file='/path/to/image.png') canvas.create_image(10, 10, image=my_image, anchor='nw')  root.mainloop() 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -