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

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

    Error Changing Rectangle Coordinates


    Here is the end-part of the error when I run a function to change
    rectangle coordinates:
    File "C:\Python33\lib\tkinter\__init__.py", line 81, in _cnfmerge
    for k, v in c.items():
    AttributeError: 'int' object has no attribute 'items'

    See the code below. What am I doing wrong in the function?
    Thanks.
    Code:
    import tkinter as tkinter
    from tkinter import *
    root = tkinter.Tk()
    
    class myrectangle():
    
        def __init__(self, root):
    
            self.rect_coords1 = [100, 30, 120, 50]
            self.rect_coords2 = [110, 40, 130, 60]
            
            self.canvas = canvas = tkinter.Canvas(root,
                width=400, height=400, bg='gray70')
            self.canvas.pack()
    
            self.canvas.create_rectangle(self.rect_coords1, fill='Yellow', tags='rect1')
                                        
            self.btn1 = Button(root, text='Change Coordinates', font=('Helvetica', 12),
                padx=3, pady=2, command=self.change_coords)
            self.btn1.place(x=160, y=450)
    
        def change_coords(self):
            self.canvas.itemconfig('rect1', self.rect_coords2)
            
    root.geometry('500x500+20+20')
    app = myrectangle(root)
    root.mainloop()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480

    This works


    Rewriting the change_coords method fixes the trouble.
    Code:
    # python3
    
    from tkinter import *
    
    class myrectangle():
    
        def __init__(self, root):
    
            self.rect_coords1 = [100, 30, 120, 50]
            self.rect_coords2 = [110, 40, 130, 60]
            
            self.canvas = canvas = Canvas(root,
                width=400, height=400, bg='gray70')
            self.canvas.pack()
    
            self.canvas.create_rectangle(self.rect_coords1, fill='Yellow', tags='rect1')
                                        
            self.btn1 = Button(root, text='Change Coordinates', font=('Helvetica', 12),
                padx=3, pady=2, command=self.change_coords)
    
            self.btn1.place(x=160, y=450)
    
            print(dir(self.canvas))
    
        def change_coords(self):
            self.canvas.coords('rect1', *self.rect_coords2)#########
            
    
    root = Tk()
    root.geometry('500x500+20+20')
    app = myrectangle(root)
    root.mainloop()
    [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
    76
    Rep Power
    2
    It appears to me that your solution uses the coords function in
    tkinter's __init__ module, and tkinter expects the asterisk in front
    of whatever is being sent to coords, whether it is a reference to a
    list, or the list itself, i.e., *self([110, 40, 130, 60] worked as well.
    Thank you again b4...

    edit: oops, that description of sending a list of args is in error,
    as b4... pointed out. More like this:
    Code:
    self.canvas.coords('rect1', *[110, 40, 130, 60])
    Last edited by pyJer; October 24th, 2013 at 12:32 PM.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Hard to tell just what you mean since you responded with a syntactical mistake.
    Code:
    # let f be a function taking 3 arguments
    
    A = 1, 2, 3
    
    f(*A)      # is equivalent to 
    f(1, 2, 3)
    
    
    # http://docs.python.org/3/reference/expressions.html#grammar-token-call
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo