python - How can I avoid "Object currently drawn" error? -
i want write python program shows letter in graphics window. if click right side of window, text needs turn red , if click left side needs turn green. needs work @ least 5 times. write down following change color 2 times , gives me "graphics.graphicserror: object drawn". idea how fix problem?
from graphics import * def main(): win= graphwin("name",400,400) win.setcoords(0.0,0.0,4.0,4.0) win.setbackground("white") p=text(point(2.0,2.0),'b') p.setsize(36) in range(0,6): c=win.getmouse() s=c.getx() if s>=2 : p.settextcolor("red") else: p.settextcolor("green") p.draw(win) main()
i new this. used zelle graphics module this
the problem position of p.draw(win)
call @korefn suggests. however, change makes 'b' visible before first click unlike original code. i've included commented out code in rework below make 'b' same color background until clicked:
from graphics import * def main(): win = graphwin('mouse test', 400, 400) win.setcoords(0.0, 0.0, 4.0, 4.0) anchorpoint = point(2.0, 2.0) text = text(anchorpoint, 'b') text.setsize(36) # maximum legal size # text.settextcolor('white') # optional initial invisibility text.draw(win) _ in range(6): point = win.getmouse() if point.getx() >= anchorpoint.getx(): text.settextcolor('red') else: text.settextcolor('green') win.close() main()
Comments
Post a Comment