Python 巨蟒龟bgpic立即消失
我试图在PyCharm中运行代码,当我使用下面的代码时,空白窗口会立即打开和关闭Python 巨蟒龟bgpic立即消失,python,python-3.x,pycharm,turtle-graphics,Python,Python 3.x,Pycharm,Turtle Graphics,我试图在PyCharm中运行代码,当我使用下面的代码时,空白窗口会立即打开和关闭 import turtle screen = turtle.Screen() screen.setup(600, 400) screen.bgpic('map.png') 这是一个问题还是不完整的海龟?这一切都是全新的 下面是关于turtle.bgpic()的帮助文本。阅读时,您会发现只有*.gif文件支持turtle窗口背景图片: >>> help(turtle.bgpic) Help on
import turtle
screen = turtle.Screen()
screen.setup(600, 400)
screen.bgpic('map.png')
这是一个问题还是不完整的海龟?这一切都是全新的 下面是关于
turtle.bgpic()
的帮助文本。阅读时,您会发现只有*.gif文件支持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'
>>>
首先将map.png
文件转换为GIF文件,这样会更好。第二,海龟代码通常需要一个最终语句,如:
turtle.mainloop()
或者它的同伴之一(
done()
,exitonclick()
)。这会将控制权移交给tkinter事件处理程序。在这样一个不处理任何事件的程序中,它仍然是必需的,因为程序只是从脚本的底部掉下来,否则就会退出。有些环境,如IDLE,不需要调用mainloop()
,但如果有疑问,可以将其包括在内。下面是turtle.bgpic()
上的帮助文本。阅读时,您会发现只有*.gif文件支持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'
>>>
首先将map.png
文件转换为GIF文件,这样会更好。第二,海龟代码通常需要一个最终语句,如:
turtle.mainloop()
或者它的同伴之一(
done()
,exitonclick()
)。这会将控制权移交给tkinter事件处理程序。在这样一个不处理任何事件的程序中,它仍然是必需的,因为程序只是从脚本的底部掉下来,否则就会退出。有些环境,如IDLE,不需要调用mainloop()
,但如果有疑问,可以将其包括在内。@ArturAlbertHamelak,在PyCharm环境中,您可能需要一个turtle.mainloop()
作为代码中的最后一条语句。它起作用了。如果你能写下来作为回答,我可以接受。你能解释一下为什么PyCharm中需要turtle.mainloop()
吗?@ArturAlbertHamelak,我已经根据你的要求在我的答案末尾添加了mainloop()
解释。PNG与.GIF部分仍然适用。@ArturAlbertHamelak,在PyCharm环境中,您可能需要一个turtle.mainloop()
作为代码中的最后一条语句。它起作用了。如果你能写下来作为回答,我可以接受。你能解释一下为什么PyCharm中需要turtle.mainloop()
吗?@ArturAlbertHamelak,我已经根据你的要求在我的答案末尾添加了mainloop()
解释。PNG与GIF部分仍然适用。