问题
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'
回答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
来源:https://stackoverflow.com/questions/39740137/how-to-undraw-plot-with-zelle-graphics