简体   繁体   English

如何避免出现“当前绘制对象”错误?

[英]How can I avoid “Object currently drawn” error?

I want to write a Python program which will shows a letter in a graphics window. 我想编写一个Python程序,它将在图形窗口中显示一个字母。 If I click the right side of the window, the text needs to turn red and if I click the left side it needs to turn green. 如果单击窗口的右侧,则文本需要变为红色,如果单击左侧,则需要变为绿色。 It needs to work for at least five times. 它至少需要工作五次。 I write down following which change color only two times and then gives me "graphics.GraphicsError: Object currently drawn". 我写下来,它仅改变颜色两次,然后给出“ graphics.GraphicsError:当前绘制的对象”。 Any idea how to fix this 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)
    for i 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 am new to this. 我是新来的。 I used zelle graphics module to do this 我用zelle图形模块来做到这一点

The problem is the position of the p.draw(win) call as @korefn suggests. 问题是@korefn建议的p.draw(win)调用的位置。 However, this change makes 'B' visible before the first click unlike your original code. 但是,与原始代码不同,此更改使“ B”在首次单击之前可见。 I've included some commented out code in my rework below that will make the 'B' the same color as the background until clicked: 在下面的重做中,我包括了一些注释掉的代码,这些代码将使'B'与背景具有相同的颜色,直到单击为止:

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)

    for _ in range(6):
        point = win.getMouse()

        if point.getX() >= anchorPoint.getX():
            text.setTextColor('red')
        else:
            text.setTextColor('green')

    win.close()

main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM