Cosmic Dust
Cosmic Dust

Reputation: 11

How to print txt ['text'] in canvas in tkinter

I want to print the text of

txt=canvas.create_text (...,text='this is some text') 

I want the program to print 'this is some text ' I used

print (txt['text']) 

it shows an error: int object is not subscriptable

Upvotes: 1

Views: 216

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385980

canvas.create_text returns an integer. That is why you get the error int object is not subscriptable.

You can use the itemcget method to get the value of an attribute of an object on the canvas. The first argument is a tag, or the id returned from one of the create_ methods. The second argument is the name of the attribute.

In your specific example it would look like this:

print(canvas.itemcget(txt, "text"))

Upvotes: 2

Related Questions