龟图形 - 尝试获取背景图像失败?

问题描述 投票:0回答:1

我一直在使用龟图形(python和pycharm),并且已经到了我想要包含背景图像的点。当我尝试使用代码时

turtle.bgpic("bkgrd.jpg")

它给了我这个错误:

couldn't recognize data in image file "bkgrd.jpg"

这是代码片段(不是整个代码):

import turtle

t = turtle.Pen()
turtle.setup(500,500)
turtle.bgpic("bkgrd.jpg")
turtle.exitonclick()

没有行turtle.bgpic(),这将运行并创建一个空白窗口。我也用gif和png尝试过这个。图形文件位于python文件的同一目录中(使用pycharm)。有什么想法吗?

python background turtle-graphics
1个回答
1
投票

返回使用* .gif文件:

> python3
Python 3.6.4 (v3.6.4:d48ecebad5, Dec 18 2017, 21:07:28) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import turtle
>>> 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'

>>> 

从文档中可以看出它是GIF还是什么都没有。如果您的* .gif文件不起作用,请使用修改后的代码(使用* .gif文件)和* .gif文件本身的链接更新您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.