Thread: Cloning objects

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

    Join Date
    Feb 2005
    Posts
    11
    Rep Power
    0

    Cloning objects


    Hi,

    I have been having trouble trying to clone an object in particular a square. I want the program to produce new squares after a user clicks in the window. Here is what I have so far. Thanks for any help.

    Code:
    from graphics import *
    
    def main():
    
    win = GraphWin()
    shape = Rectangle(Point(50,50), Point(100,100))
    shape.setOutline("red")
    shape.setFill("red") shape.draw(win) output = Text(Point(100,125), "Click again to quit") output.draw(win) win.getMouse() win.close()
    main()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    There is a copy module which provides functions for cloning objects (see the current python documentation for details).

    To clone a square, try this:
    Code:
    import copy
    ...
    ...
        new_shape = copy.deepcopy(shape)
    ...
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11
    or, you could do something like this (i think this would work):

    Code:
    shape2 = shape
    but, that may just modify the first one

    another way is though the copy module, which sbkwi describes.

IMN logo majestic logo threadwatch logo seochat tools logo