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

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

    Delete a created shape from canvas


    How is a shape on a canvas removed?
    In this code, I want the current shape to disappear when
    a new shape is created.
    Thanks.
    Code:
    try:
        from Tkinter import *     ## Python 2.x
    except ImportError:
        from tkinter import *     ## Python 3.x
    
    root = Tk()
    root.geometry('505x555+20+20')
    
    class myshapes():
       
        def __init__(self, root):
    
            self.btn1 = Button(root, text='Circle', font=('Helvetica', 12),
                command=self.draw_oval)
            self.btn1.pack()
            self.btn1.place(x=120, y=5)
    
            self.btn2 = Button(root, text='Triangle', font=('Helvetica', 12),
                command=self.draw_triangle)
            self.btn2.pack()
            self.btn2.place(x=10, y=5)
           
            self.canvas = Canvas(root, width=500, height=500, bg='Azure')    
            
            self.draw_triangle()
            self.canvas.pack()
            self.canvas.place(y=50)
        
        def draw_oval(self):
            points = self.oval_points()
            p = self.canvas.create_oval(points, outline='Cyan', fill='Cyan')
                
        def draw_triangle(self):   
            points = self.triangle_points()
            p = self.canvas.create_polygon(points, outline='grey50', fill='grey50')
    
        def oval_points(self):
            x0=350
            y0=350
    
            x1=100
            y1=100
    
            return(x0,y0,x1,y1)
        
        def triangle_points(self):    
            lx=200
            ly=250
    
            hx=250
            hy=200
            
            rx=300
            ry=250
            
            return(lx,ly,hx,hy,rx,ry)
    
    
    ms = myshapes(root)
    
    root.mainloop()
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    Looks like you need the canvas_object.delete(item) method.

    delete(item) [#]
    Deletes all matching items. It is not an error to give an item specifier that doesn’t match any items.

    item
    Item specifier (tag or id).
    [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
    Your reply 'IT is not an error...', seems to be telling me that I
    should not get an error trying to delete a canvas item that does
    not exist. Unless I use Try...Except, as in the code posted here,
    I get this error with Python 3.3 in the first run:
    self.canvas.delete(self.p)
    AttributeError: 'myshapes' object has no attribute 'p'

    Code:
    try:
        from Tkinter import *     ## Python 2.x
    except ImportError:
        from tkinter import *     ## Python 3.x
    
    root = Tk()
    root.geometry('505x555+20+20')
    
    class myshapes():
       
        def __init__(self, root):
    
            self.btn1 = Button(root, text='Circle', font=('Helvetica', 12),
                command=self.draw_oval)
            self.btn1.pack()
            self.btn1.place(x=120, y=5)
    
            self.btn2 = Button(root, text='Triangle', font=('Helvetica', 12),
                command=self.draw_triangle)
            self.btn2.pack()
            self.btn2.place(x=10, y=5)
           
            self.canvas = Canvas(root, width=500, height=500, bg='Azure')    
            
            self.draw_triangle()
            self.canvas.pack()
            self.canvas.place(y=50)
        
        def draw_oval(self):
            try:
                self.canvas.delete(self.p)
            except:
                pass
            
            points = self.oval_points()
            self.p = self.canvas.create_oval(points, outline='Cyan', fill='Cyan')
                
        def draw_triangle(self):
            try:
                self.canvas.delete(self.p)
            except:
                pass
            
            points = self.triangle_points()
            self.p = self.canvas.create_polygon(points, outline='grey50', fill='grey50')
    
        def oval_points(self):
            x0=350
            y0=350
    
            x1=100
            y1=100
    
            return(x0,y0,x1,y1)
        
        def triangle_points(self):    
            lx=200
            ly=250
    
            hx=250
            hy=200
            
            rx=300
            ry=250
            
            return(lx,ly,hx,hy,rx,ry)
    
    
    ms = myshapes(root)
    
    root.mainloop()
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    I didn't read or try your code. Sorry dudette or dude.

    Still only reading your opening 4 lines as a paragraph...I might try harder tonight...

    reconfiguring the item rather than delete and recreate might be what you want.

    canvas_object.itemconfig

    Originally Posted by effbot.org
    Note that items added to the canvas are kept until you remove them. If you want to change the drawing, you can either use methods like coords, itemconfig, and move to modify the items, or use delete to remove them.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    78
    Rep Power
    2
    What I am doing is just to add to my skills. I have a canvas and
    sliders for changing parameters that go into creating a polygon
    shape.
    I want to see how each parameter affects the shape, size,
    and possibly location on the canvas.

    After I set up the module, I figured I probably needed to delete
    the shape each time there is a re-draw. So that's where this
    dude is coming from.
    Jerry
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    No, you don't---ehh shouldn't--- delete and recreate. Use itemconfig(keyword=arguments)
    There also seems to be a specific function to change coordinates. For example,
    Code:
    $ python3
    Python 3.3.1 (default, Sep 25 2013, 19:29:01) 
    [GCC 4.7.3] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import tkinter
    >>> c=tkinter.Canvas()
    >>> (c.create_rectangle)(1,1,40,40)
    1
    >>> c.pack()
    >>> c.coords(1)   # shows coordinates
    [1.0, 1.0, 40.0, 40.0]
    >>> c.coords(1, 40,50,200,400)  # replace coordinates on item 1
    []
    >>>
    Run these lines of code. If your system is set up reasonably, you'll see the rectangle move.
    [code]Code tags[/code] are essential for python code and Makefiles!
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    78
    Rep Power
    2
    I tried your interactive code and it worked as expected. How do I
    put this in a script? I get an error about 'coords' that says
    'non-keyword arg after keyword arg', and I've tried it about every
    way there is except the correct way. At your suggestion, my
    goal is to not have to delete the shape every time there is a
    change to the coordinates.
    I commented out the problem line in this code:
    Code:
    try:
        from Tkinter import *     ## Python 2.x
    except ImportError:
        from tkinter import *     ## Python 3.x
    
    root = Tk()
    root.geometry('300x355+20+20')
    
    class myshapes():
        
        def __init__(self, root):
    
            self.rectangle_coords = [0.0, 0.0, 10.0, 10.0]
            
            self.btn1 = Button(root, text='Click Me', font=('Helvetica', 12),
                command=self.draw_rectangle)
            self.btn1.pack()
            self.btn1.place(x=120, y=5)
    
            self.canvas = Canvas(root, width=300, height=300, bg='Azure')    
            
            self.draw_rectangle()
            
            self.canvas.pack()
            self.canvas.place(y=50)
        
        def draw_rectangle(self):
    
            self.canvas.delete('shp')
            self.p = self.canvas.create_rectangle(self.rectangle_coords,
                outline='grey80', fill='grey80')
            self.canvas.itemconfig(self.p, tags='shp')
    
            self.canvas.itemconfig(tagOrId='shp', fill='cyan')  #no error here
            #self.canvas.itemconfig(tagOrId='shp', coords(20.0, 20.0, 50.0, 50.0))  # error raised
            
            self.rectangle_coords = \
                [(self.rectangle_coords[0]+80) % 320, \
                (self.rectangle_coords[1]+80) % 320, \
                self.rectangle_coords[2], \
                self.rectangle_coords[3]]
                 
    ms = myshapes(root)
    
    root.mainloop()
    Last edited by pyJer; October 9th, 2013 at 02:02 PM.
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    Code:
    import sys
    if sys.version[0] == '3':
        import tkinter
    else:
        import Tkinter as tkinter
    
    class myshapes:
    
        def __init__(self, root):
    
            self.rectangle_coords = (0.0, 0.0, 10.0, 10.0)
    
            tkinter.Button(root,text='Click Me',font=('Helvetica', 12),
                           command=self.move_rectangle).pack()
    
            self.canvas = canvas = tkinter.Canvas(root,
                                                  width=300,
                                                  height=300,
                                                  bg='Azure',)
    
            canvas.create_rectangle(self.rectangle_coords,
                                    outline='grey80',
                                    fill='cyan',
                                    tags='shp',)
    
            canvas.pack()
    
        def move_rectangle(self):
            c = self.rectangle_coords
            self.canvas.coords('shp', *c) # move the rectangle
            self.rectangle_coords = ((c[0]+80) % 320, (c[1]+80) % 320, c[2], c[3])
    
    root = tkinter.Tk()
    root.geometry('300x355+20+20')
    ms = myshapes(root)
    root.mainloop()
    The code is ugly. Sorry.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo