0

This is a code problem for Python 3.5.2 using John Zelle's graphics.py:

I have spent a good amount of time looking for the answer here, but just can not figure it out. The function undraw() exists just like getMouse(). But it seems like it do not work for the plot() command, only the draw() command. What am I doing wrong? And how can I keep the window open, but erase the previous the plot before the next one is drawn?

pdf documentation for the functions of graphics:

    http://mcsp.wartburg.edu/zelle/python/graphics/graphics.pdf

win = GraphWin("Plot",500,500) # Creates a window

for m in range(0,j): # Loop for each function
    # Randomizes a color for each function
    color = random.choice( ['red','black','green','yellow','pink','blue'] )
    for h in range(0,t): # Loop for each pair of values "x,y"
        # Find points and plot each point in win
        win.plot(axis[h],points[m][h],color)
    win.getMouse() # Pause before clicking
    win.undraw() # AttributeError: 'GraphWin' object has no attribute 'undraw'
martineau
  • 119,623
  • 25
  • 170
  • 301
brolija
  • 21
  • 7
  • 1
    Not enough information ... so +Close for now. Add things like: what language? what lib? what exactly is `win` and where it is defined/declared ? – Spektre Sep 28 '16 at 07:28

1 Answers1

1

The first issue is that undraw() is a method of GraphicsObject, not GraphWin, so win.undraw() is simply incorrect.

The second issue is that plot() is a low level pixel setting method that does not keep track of what it did at the Zelle Graphics level, unlike objects that are drawn.

However, the underpinning is Tkinter which does keep track of objects that it draws, and GraphWin is a subclass of Canvas, so you can do:

win = GraphWin("Plot", 500, 500) # Creates a window

for m in range(j):  # Loop for each function
    color = random.choice(['red', 'black', 'green', 'yellow', 'pink', 'blue']) # Randomizes a color for each function
    for h in range(t):  # Loop for each pair of values "x, y"
        win.plot(axis[h], points[m][h], color) # Find points and plot each point in win
    win.getMouse()  # Pause before clicking
    win.delete("all")  # Clear out old plot
cdlane
  • 40,441
  • 5
  • 32
  • 81
  • I think your answer would be clearer if you indicated that the `win.delete("all")` was calling the inherited `Canvas.delete()` method (and passing it the special predefined tag `"all"`). Folks may not realize that `GraphWin` doesn't have its own definition of a method by that name. – martineau Aug 07 '17 at 15:03