2

I am trying to figure out away to append text to tkinter objects. I have 64 text objects in total. I assigns text to an object as follows:

self.canvas.itemconfig(self.TextKeys[3], text = "Image")

But in the case I want to do:

self.canvas.itemconfig(self.TextKeys[3], text = "Comm")

How can I add the text instead of replacing it? Another issue I have is the amount of space on the canvas object is limted: enter image description here

Any advice/suggestions will be greatly appreciated!

4
  • can you use a variable to always keep track of what the text is/should be and then replace the text with the value of that variable? Commented Aug 8, 2012 at 19:59
  • or try ... old_text = self.TextKeys[3]["text"] and then add something to old_text and replace the text with the modified value. Commented Aug 8, 2012 at 20:04
  • That's not a bad idea. So I would need to create a variable for all 64 text objects, or maybe just create a dictionary? I'll try a few different things. Any idea on the display issue? Commented Aug 8, 2012 at 20:47
  • @NASAIntern Your image link is broken. Commented Aug 9, 2012 at 2:01

1 Answer 1

2

You can use the insert canvas method to insert something into a string object. Since you want to append, use the index "end", eg:

self.canvas.insert(self.TextKeys[3], "end", "this text will be appended")

I don't understand your question about the amount of space being limited. The amount of space an item takes up is configurable by your code.

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

5 Comments

tagNumber = 0 for i in range(8): x = -5 + 3*i y = -3 for j in range(8): self.canvas.create_rectangle('%dc'%x,'%dc'%y, '%dc'%(x+2), '%dc'%(y+2), outline='black', fill= 'black', tags=(tagNumber, "tile")) self.canvas.create_text('%dc'%(x+1),'%dc'%(y+1),text ='%d'% (tagNumber), fill = 'white' anchor=CENTER, tags=('text', 'text')) tagNumber = tagNumber + 1 y = y + 3
Your advice is great! I believe that will work. I just need to check to see if the string is empty first, if not, then append. But the code I pasted above explains the space issue. Basically it creates 64 rectangles using the 2 loops and uses the same variables (x, y) to create the position of the text objects. My space is limited to these parameters. As you can see in the original image I posted, it creates rectangles of that size.
How is the amount of space determined for the text? Can I force the appended text to appear below the original? Or would I need a whole new text object with that specific location? I need to be able to somewhat contain the text in the rectangle. But putting it below or above wouldnt be bad either.
@NASAIntern One way to check the current text is to retrieve the config dictionary: data = self.canvas.itemconfigure(id) and then data['text'][-1]
what does data['text'][-1] do? And data = self.canvas.itemconfigure(id) returns the string?

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.