当前位置:   article > 正文

PyCharm中from turtle import *失效的解决办法_from turtle import *报错

from turtle import *报错

在用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']
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
'
运行

保存后,在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()      #画布悬停
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

from turtle import * 和 done() 一起使用

**注意:**这两种用法不能混用,要不然无效。

(这是第一次写,写的不好勿怪!)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/833389
推荐阅读
相关标签
  

闽ICP备14008679号