【问题标题】:How to hide a turtle icon/pointer in Python如何在 Python 中隐藏海龟图标/指针
【发布时间】:2015-12-24 14:35:25
【问题描述】:

使用 Python Turtle 时,如何在 Turtle 代码中的海龟图形中隐藏海龟图标/指针,使其在测试时不显示?

【问题讨论】:

    标签: python visibility turtle-graphics


    【解决方案1】:

    另一个答案未能解决的更实用的方法是在定义Turtle 对象时将visible 关键字参数设置为False

    import turtle
    
    my_turtle = turtle.Turtle(visible=False)
    

    当然,那是在您希望 Turtle 从程序一开始就看不见的时候。

    当您定义 Turtle 对象而不将 visible 设置为 False 时,总会有一个闪电般的短暂时刻,乌龟仍然可见:


    import turtle
    
    my_turtle = turtle.Turtle()
    # The Turtle may be visible before the program reaches the line under, depending on the speed of your computer 
    my_turtle.hideturtle()
    

    visible 关键字参数设置为False,您可以随时在代码中调用my_turtle.showturtle()my_turtle.hideturtle(),只要Turtle 需要再次可见和隐藏。


    这里是您可以自定义的所有默认turtle 设置(这里感兴趣的设置是带有# RawTurtle 注释的设置)

    _CFG = {"width" : 0.5,               # Screen
            "height" : 0.75,
            "canvwidth" : 400,
            "canvheight": 300,
            "leftright": None,
            "topbottom": None,
            "mode": "standard",          # TurtleScreen
            "colormode": 1.0,
            "delay": 10,
            "undobuffersize": 1000,      # RawTurtle
            "shape": "classic",
            "pencolor" : "black",
            "fillcolor" : "black",
            "resizemode" : "noresize",
            "visible" : True,
            "language": "english",        # docstrings
            "exampleturtle": "turtle",
            "examplescreen": "screen",
            "title": "Python Turtle Graphics",
            "using_IDLE": False
           }
    

    更新:我刚刚注意到 cdlane 对另一个答案的评论指出了这种方法,但 cmets 是临时的。

    【讨论】:

      【解决方案2】:

      documentation 有一个关于Visibility 的部分:

      turtle.hideturtle()
      turtle.ht()
      使乌龟隐形。当你在做一些复杂的绘图时这样做是个好主意,因为隐藏海龟可以明显加快绘图速度。

      >>> turtle.hideturtle()
      

      另外,您可以取消隐藏海龟:

      turtle.showturtle()
      turtle.st()
      让乌龟可见。

      >>> turtle.showturtle()
      

      您也可以查询它的可见性:

      turtle.isvisible()
      如果 Turtle 显示,则返回 True,如果隐藏则返回 False

      >>> turtle.hideturtle()
      >>> turtle.isvisible()
      False
      >>> turtle.showturtle()
      >>> turtle.isvisible()
      True
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 2014-03-18
        • 1970-01-01
        • 2013-10-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多