配置信息
Macbook pro
Mac OS Mojave:10.14.16
Python version:3.7.6 (Anaconda 安装)
代码
import turtle as window
window.pensize(4)
window.pencolor('red')
window.forward(100)
window.right(90)
window.forward(100)
window.right(90)
window.forward(100)
window.right(90)
window.forward(100)
window.mainloop()
初次运行,电脑闪退并重启,重新登陆后信息丢失,无法获知错误信息。经过下述代码测试发现,电脑闪退原因为python自带库tkinter无法正常载入所致。运行下述代码也会导致电脑闪退。
import tkinter
top = tkinter.Tk()
top.mainloop()
因此,必须通过修复tkinter来修复turtle的闪退问题。根据Python官网描述(参考链接),部分Mac系统自带Python的Tcl/tk存在多个bug,用户可以通过重新安装Python和安装Tcl来修复bug。
由于没能顺利下载Tcl安装包且Python来自于第三方Anaconda,因此采取更换Python版本的方式来进行修复。实际应用发现,3.7.0版本下,tkinter和turtle模块均能正常运行。
正常运行结果:

部分猜测:
尽管Python官网只说明部分Mac电脑自带Python存在上述问题,但我认为anaconda安装的python部分版本也存在tkinter的问题。
参考链接:
Mac OS Mojave+python3.6安装tkinter
配置信息Macbook proMac OS Mojave:10.14.16Python version:3.7.6 (Anaconda 安装)代码import turtleturtle.pensize(4)turtle.pencolor('red')turtle.forward(100)turtle.right(90)turtle.forward(100)turtle.r...
# -*- coding: UTF-8 -*-
from tkinter.constants import BOTH, E, END, INSERT, LEFT, N, TOP, W, X, YES
from turtle import color
from PIL import ImageGrab
from time import slee
最近学习python,用matplotlib库画图,运行程序后,就会闪退。解决办法:python2.*后面加上:raw_input()python3.*后面加上:input()我的python是python3.2,最后需加input,如下:如果你是3.*后面加的raw_input(),就会出现如下错误:
转载于:https://blog.51cto....
python turtle库是一个绘图库,可以让你在python中使用turtle来绘制图形。它的使用方法非常简单,可以帮助初学者学习编程的基础知识。
首先,需要在你的python环境中安装turtle库。这可以通过在终端中使用命令 "pip install turtle" 来完成。
然后,在你的python代码中导入turtle模块。这可以通过使用 "import turtle" 来完成。
接下来,你就可以使用turtle中的方法来控制小海龟的移动和绘制图形了。例如,你可以使用 "turtle.forward(distance)" 来让海龟向前移动一段距离,使用 "turtle.left(angle)" 来让海龟向左转一个角度,使用 "turtle.right(angle)" 来让海龟向右转一个角度,使用 "turtle.pencolor(color)" 来设置海龟的画笔颜色,使用 "turtle.pensize(size)" 来设置海龟的画笔粗细等。
你还可以使用turtle库中的其他方法来绘制复杂的图形,例如圆、五边形、六边形等。
示例代码:
import turtle
# 设置海龟的速度
turtle.speed(1)
# 循环绘制五边形
for i in range(5):
turtle.forward(100)
turtle.right(72)
# 等待用户点击窗口关闭
turtle.exitonclick()
运行上面的代码会弹出一个窗口,显示海龟绘制的