赞
踩
在用Python编写绘图程序时,我们会用到 import turtle 和 from turtle import * 两个语句,但在PyCharm2017编写程序时,会发现后者无法使用,接下来我会讲解我使用的解决办法。
找到Python解释器的安装文件,打开Lib并找到 turtle.py 库
右击以记事本打开,如下图,滑到这个界面,红色框的内容用#注释掉,蓝色框是我后面添加的,代码会在后面给出。
蓝色框中的代码:
__all__ = ['ScrolledCanvas', 'TurtleScreen', 'Screen', 'RawTurtle', 'Turtle', 'RawPen', 'Pen', 'Shape', 'Vec2D', 'back',
'backward', 'begin_fill', 'begin_poly', 'bk', 'addshape', 'bgcolor', 'bgpic', 'bye', 'clearscreen',
'colormode', 'delay', 'exitonclick', 'getcanvas', 'getshapes', 'listen', 'mainloop', 'mode', 'numinput',
'onkey', 'onkeypress', 'onkeyrelease', 'onscreenclick', 'ontimer', 'register_shape', 'resetscreen',
'screensize', 'setup', 'Terminator', 'setworldcoordinates', 'textinput', 'title', 'tracer', 'turtles',
'update', 'window_height', 'window_width', 'write_docstringdict', 'done', 'circle', 'clear', 'clearstamp',
'clearstamps', 'clone', 'color', 'degrees', 'distance', 'dot', 'down', 'end_fill', 'end_poly', 'fd',
'fillcolor', 'filling', 'forward', 'get_poly', 'getpen', 'getscreen', 'get_shapepoly', 'getturtle', 'goto',
'heading', 'hideturtle', 'home', 'ht', 'isdown', 'isvisible', 'left', 'lt', 'onclick', 'ondrag', 'onrelease',
'pd', 'pen', 'pencolor', 'pendown', 'pensize', 'penup', 'pos', 'position', 'pu', 'radians', 'right', 'reset',
'resizemode', 'rt', 'seth', 'setheading', 'setpos', 'setposition', 'settiltangle', 'setundobuffer', 'setx',
'sety', 'shape', 'shapesize', 'shapetransform', 'shearfactor', 'showturtle', 'speed', 'st', 'stamp', 'tilt',
'tiltangle', 'towards', 'turtlesize', 'undo', 'undobufferentries', 'up', 'width', 'write', 'xcor', 'ycor']
保存后,在PyCharm2017再次编辑,问题就解决了,这样 import turtle 和 from turtle import * 两个语句就都可以使用了。
第一种用法:
import turtle # 引用turtle库
turtle.pensize(2) # 设置画笔宽度为2像素
turtle.color("red")#设置画笔颜色
n=5
while n<100:
turtle.circle(n)
n=n+20
turtle.done() #画布悬停
import turtle 和 turtle.done()一起使用
第二种用法:
from turtle import *
fillcolor("yellow")
color("yellow")
begin_fill()
while True:
forward(180)
right(144)
if abs(pos())<1:
break
end_fill()
done()
from turtle import * 和 done() 一起使用
**注意:**这两种用法不能混用,要不然无效。
(这是第一次写,写的不好勿怪!)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。