How to undraw plot with Zelle graphics?

删除回忆录丶 提交于 2020-01-17 12:41:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!