pythonturtle怎么导入背景_python中的Turtle模块没有导入

这是我第一次在python中使用turtle模块,但我似乎无法导入它?

这是我的代码:

from turtle import *

pen1 = Pen()

pen2 = Pen()

pen1.screen.bgcolour("#2928A7")

这是我得到的错误:

Traceback (most recent call last):

File "C:\Python34\Python saves\turtle.py", line 2, in

from turtle import *

File "C:\Python34\Python saves\turtle.py", line 5, in

pen1 = Pen()

NameError: name 'Pen' is not defined

谁能告诉我我做错了什么?

解决方法:

问题是你已经将你的程序命名为“turtle.py”.

所以当Python看到这个陈述时

来自龟进口*

它找到的第一个名为turtle的匹配模块是你的程序“turtle.py”.

换句话说,您的程序基本上是导入自己而不是乌龟图形模块.

这里有一些代码来演示这个问题.

turtle.py

#! /usr/bin/env python

''' Mock Turtle

Demonstrate what happens when you give your program the same name

as a module you want to import.

See https://stackoverflow.com/q/32180949/4014959

Written by PM 2Ring 2015.08.24

'''

import turtle

foo = 42

print(turtle.foo)

help(turtle)

我想我应该展示代码实际打印的内容……

当以turtle.py运行时,它会打印以下“帮助”信息:

Help on module turtle:

NAME

turtle - Mock Turtle

FILE

/mnt/sda4/PM2Ring/Documents/python/turtle.py

DESCRIPTION

Demonstrate what happens when you give your program the same name

as a module you want to import.

See https://stackoverflow.com/q/32180949/4014959

Written by PM 2Ring 2015.08.24

DATA

foo = 42

(END)

当您点击Q退出帮助时,会再次显示帮助信息.当你第二次点击Q时,那么

42

42

打印出来.

为什么“帮助”消息和42打印两次?这是因为turtle.py中的所有代码都是在导入时执行的,然后在import语句后遇到它时再次执行.请注意,Python不会尝试导入已导入的模块(除非明确告知重新加载).如果Python重新导入,那么上面的代码将陷入无限的导入循环.

当以mockturtle.py运行时,它会打印:

Traceback (most recent call last):

File "./mock_turtle.py", line 16, in

print(turtle.foo)

AttributeError: 'module' object has no attribute 'foo'

当然那是因为标准的海龟模块实际上没有foo属性.

标签:python,turtle-graphics

来源: https://codeday.me/bug/20190623/1272680.html

### 回答1: 要在Python Turtle导入背景,可以使用以下步骤: 1. 首先,您需要选择要用作背景的图像。您可以在互联网上搜索免费的图像,或者使用自己的图像。 2. 然后,您需要将图像文件保存在您的Python程序所在的文件夹。 3. 接下来,您需要使用Pythonturtle模块导入图像。您可以使用以下代码: ```python import turtle # 创建一个画布 canvas = turtle.Screen() # 设置背景图像 canvas.bgpic("your_image_file_name.gif") # 运行程序 turtle.done() ``` 在上面的代码,您需要将“your_image_file_name.gif”替换为您选择的图像文件的名称。 4. 最后,您需要运行程序以查看导入背景。您可以使用以下代码: ```python turtle.done() ``` 这将使程序保持运行状态,直到您关闭窗口为止。 希望这可以帮助您导入背景图像到Python Turtle。 ### 回答2: PythonTurtle 是一个基于 Python 的开源绘图框架,它允许用户使用 Turtle 这个类来进行绘制。如果想要为 PythonTurtle 绘制的图形添加背景,需要导入背景图像来实现。 PythonTurtle 提供了内置的方法 `bgpic()` 来导入背景图像,在使用这个方法之前,需要首先安装并导入 `pillow` 模块。然后,可以使用 `bgpic()` 方法来导入图像文件: ``` import turtle # 设置画布 turtle.setup(800, 600) # 导入背景图像 turtle.bgpic("my_bg_image.gif") # 加载窗口 turtle.mainloop() ``` 在上面的代码,`bgpic()` 方法的参数是图像文件的路径,例如 `my_bg_image.gif` 代表在当前目录下的 `my_bg_image.gif` 文件。 如果需要在背景图像后面添加绘制的对象,可以使用 `bg()` 方法来绘制背景色。 ``` import turtle # 设置画布 turtle.setup(800, 600) turtle.bgpic("my_bg_image.gif") # 绘制一个圆 turtle.penup() turtle.goto(-200, 0) turtle.pendown() turtle.circle(100) # 添加背景turtle.bgcolor("white") # 加载窗口 turtle.mainloop() ``` 在上面的代码,`bgcolor()` 方法用于设置背景色为白色,所以在绘制完圆形之后背景色就变成了白色。 总之,在 PythonTurtle 导入背景主要使用内置的方法 `bgpic()` 和 `bgcolor()`,前者用于导入背景图像,后者用于设置背景色。导入背景图像之前需要安装并导入 `pillow` 模块。 ### 回答3: 使用Pythonturtle库绘制图形时,可以为画布添加背景图像来增强视觉效果。以下是如何使用Pythonturtle导入背景的三种方法: 方法一:在程序导入背景图像 你可以在程序导入背景图像,并将其作为画布的背景。这里需要用到Python的Pillow库来处理图像。下面是一个例子,将背景图像happy.png作为画布的背景: ``` import turtle from PIL import Image # 导入背景图像 bg_img = "happy.png" # 创建画布并设置背景图像 screen = turtle.Screen() screen.bgpic(bg_img) # 绘制图形 turtle.shape("turtle") turtle.color("red") turtle.forward(100) turtle.done() ``` 方法二:将背景图像添加到tkinter的PhotoImage对象 另一种方法是将背景图像添加到tkinter的PhotoImage对象,然后将该对象作为画布的背景。下面是一个例子,将背景图像sky.jpg添加到画布的背景: ``` import turtle import tkinter as tk # 将背景图像添加到PhotoImage对象 bg_img = "sky.jpg" img = tk.PhotoImage(file=bg_img) # 创建画布并设置背景图像 screen = turtle.Screen() screen.setup(img.width(), img.height()) # 设置画布大小为图片大小 screen.bgpic(img) # 绘制图形 turtle.shape("turtle") turtle.color("red") turtle.forward(100) turtle.done() ``` 方法三:使用turtle的addshape()方法 还有一种方法是使用turtle的addshape()方法,将背景图像添加为一个形状,然后使用该形状作为画布的背景。下面是一个例子,将背景图像flower.gif添加为形状,然后将其作为画布背景: ``` import turtle # 添加背景图像为一个形状 bg_shape = "flower.gif" turtle.addshape(bg_shape) # 创建画布并设置背景图像 screen = turtle.Screen() screen.bgpic(bg_shape) # 绘制图形 turtle.shape("turtle") turtle.color("red") turtle.forward(100) turtle.done() ``` 以上三种方法都可以用来导入背景图像,具体选择哪种方法取决于个人喜好和需要。无论哪种方法,都需确保图像文件存在,并且指定的图片文件格式是Pythonturtle支持的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值