#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    78
    Rep Power
    2

    Error trying to change text on canvas


    I need help with a text object created on canvas with the
    create_text method. When I try to change the text, this is
    the error: AttributeError: 'int' object has no attribute 'itemconfigure' See the code below.

    Thanks for your assistance.
    Code:
    import tkinter as tkinter
    root = tkinter.Tk()
    
    class text_on_canvas():
       
        def __init__(self, root):
    
            tkinter.Button(root,text='Click Me',font=('Helvetica', 12),
                command=self.letter_change).pack()
    
            self.canvas = canvas = tkinter.Canvas(root,
                width=100, height=100, bg='Azure')    
    
            self.canvas.pack()
            self.canvas.place(x=25, y=50)
    
            self.t1 = canvas.create_text(50.0, 50.0, text='A',
                font=('Helvetica', 24))
       
        def letter_change(self):
            self.t1.itemconfigure(text='B')     #error raised on this line
            
    root.geometry('160x160+20+20')
    app = text_on_canvas(root)
    root.mainloop()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,961
    Rep Power
    481
    pack and place both put widgets onto higher level container widgets. Choose to pack your widgets, choose to place your widgets, or make your own layout using the canvas. Don't use them both.

    The canvas create_FIGURE methods all return the id assigned to the collection of behaviors and picture just created. Use your canvas's itemconfig method.

    self.canvas.itemconfig(text_widget_id,
    #other keyword arguments.
    )
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    78
    Rep Power
    2
    b4... thank you for the tip on using pack OR place, not both because
    place does the packing as well as positioning.

    Your post helped me work out the code below, where I use itemconfig
    method at the canvas level to change attributes of the text
    widget. The code also includes examples of testing for the text
    widget's text value using the itemcget method.

    Code:
    import tkinter as tkinter
    root = tkinter.Tk()
    
    class text_on_canvas():
        
        def __init__(self, root):
    
            self.b1 = tkinter.Button(root,text='Click Me', width=10,
                font=('Helvetica', 12),
                command=self.letter_change)
            self.b1.pack()
    
            self.canvas = canvas = tkinter.Canvas(root,
                width=100, height=100, bg='Azure')    
    
            self.canvas.place(x=25, y=50)
    
            self.t1 = canvas.create_text(50.0, 50.0, text='A',
                font=('Helvetica', 24))
    
            self.txt_chars = 'ABC'
           
        def letter_change(self):   
            new_char = \
                self.txt_chars[self.txt_chars.find(self.canvas.itemcget(1, 'text'))+1]
    
            self.canvas.itemconfig(1, text=new_char)
                     
            if self.canvas.itemcget(1, 'text') == \
                    self.txt_chars[len(self.txt_chars)-1:len(self.txt_chars)]:
                self.b1.config(text='Exit', command=root.destroy)        
                    
    root.geometry('160x160+20+20')
    app = text_on_canvas(root)
    root.mainloop()
    Edit: redundant width=10 removed
    Last edited by pyJer; October 11th, 2013 at 12:52 PM.

IMN logo majestic logo threadwatch logo seochat tools logo