【问题标题】:Python turtle bgpic instantly disappearsPython海龟bgpic瞬间消失
【发布时间】:2019-02-20 19:48:20
【问题描述】:

我正在尝试在 PyCharm 中运行代码,当我使用空白窗口下方的代码时会立即打开和关闭。

import turtle

screen = turtle.Screen()
screen.setup(600, 400)
screen.bgpic('map.png')

这是 PyCharm 问题还是海龟不完整?对这一切都相当陌生。

【问题讨论】:

    标签: python python-3.x pycharm turtle-graphics


    【解决方案1】:

    以下是turtle.bgpic() 的帮助文本。看完你会发现,海龟窗口背景图片只支持*.gif文件:

    >>> help(turtle.bgpic)
    Help on function bgpic in module turtle:
    
    bgpic(picname=None)
        Set background image or return name of current backgroundimage.
    
        Optional argument:
        picname -- a string, name of a gif-file or "nopic".
    
        If picname is a filename, set the corresponding image as background.
        If picname is "nopic", delete backgroundimage, if present.
        If picname is None, return the filename of the current backgroundimage.
    
        Example:
        >>> bgpic()
        'nopic'
        >>> bgpic("landscape.gif")
        >>> bgpic()
        'landscape.gif'
    
    >>> 
    

    首先将您的map.png 文件转换为应该更好用的GIF 文件。其次,通常海龟代码需要一个最终语句,如:

    turtle.mainloop()
    

    或者它的同伴之一(done()exitonclick())。这会将控制权移交给 tkinter 事件处理程序。在像这样一个不对事件做任何事情的程序中,它仍然是需要的,因为程序只是从脚本的底部掉下来,否则就会退出。某些环境(例如 IDLE)不需要 mainloop() 调用,但如有疑问,请包含它。

    【讨论】:

    • @ArturAlbertHamelak,在 PyCharm 环境中,您可能需要 turtle.mainloop() 作为代码中的最后一条语句。
    • 成功了。如果您可以将其写为答案,我可以接受。您能否解释一下为什么在 PyCharm 中需要 turtle.mainloop()
    • @ArturAlbertHamelak,我已根据您的要求将mainloop() 解释添加到我的答案末尾。 PNG 与 GIF 部分仍然适用。
    猜你喜欢
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 1970-01-01
    相关资源
    最近更新 更多