0

So I am displaying an image using canvas in my Tkinter window. Now I have a button which calculates certain parameters and I want that to be displayed dynamically over the image. So basically after each calculation, the annotations should change to new parameters.

e.g. Consider that I am calculating column dimensions, and after each calculation, I have to display the dimensions marked on the respective sides of the column image that I have in my Tkinter window.

So is it possible to annotate the image dynamically and if not, is there any other way to perform this task?

1
  • Have you tried to place the image on the canvas then create the annotations over it? If you can do that and you want to update the annotations just delete the sprite and create the new annotations. Commented Feb 16, 2021 at 14:52

1 Answer 1

1

Check the below example out

from tkinter import *
from PIL import Image,ImageTk
import random

def annotate():
    annotation=str(random.randint(1000,9999))
    canvas.itemconfig(annotation_text,text=annotation)

root=Tk()

canvas=Canvas(root)
canvas.pack(fill='both',expand=True)

image=Image.open('example.png')
image=ImageTk.PhotoImage(image.resize((300,300)))
canvas.create_image(150,150,image=image)

annotation_text=canvas.create_text(50,50,text='')

button=Button(root,text='Annotate',command=annotate)
button.pack()

root.mainloop()

You can make use of the itemconfig method to configure the text. The above example will annotate the image at (50,50) with a random integer on click of a button.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that helped, btw is there any way we can do this in real-time, like simultaneously while I am entering the value, without the need to press the button?
There are multiple ways to do it, I think the easiest one would be to use widget.bind('<Key>',update) where update is the function that fetches the value and updates the item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.