我在Windows 7中安装了Python 3.7.2。当我编写turtle代码并将其保存在Python文件夹中时,它运行时没有错误。如果我将其保存在另一个文件夹中,则会返回以下错误:
NameError:未定义名称点。
这是我的代码:
from tkinter import *
from tkinter import ttk
from turtle import *
def triangle():
fd(200);left(135);fd(150);goto(0,0)
def rectangle():
lt(180);fd(250);lt(90);fd(125);lt(90);fd(250);lt(90);fd(125)
def square():
fd(250);rt(90);fd(250);rt(90);fd(250);rt(90);fd(250)
def grdk():
circle(75)
window=Tk()
window.title('GeoFigs')
window.geometry('250x200')
label=Label(window,text='Choose one figure',font='Aharoni -22 bold')
label.pack()
sekunga=ttk.Button(window,text='TRIANGLE',command=triangle)
sekunga.pack()
chor=ttk.Button(window,text='RECTANGLE',command=rectangle)
chor.pack()
kv=ttk.Button(window,text='SQUARE',command=square)
kv.pack()
grd=ttk.Button(window,text='CIRCLE',command=grdk)
grd.pack()
mainloop()
我不确定它是否与你的“NameError:name dot is not defined”问题有关,但我不会开始调试像这样设置的龟程序。 Turtle设计用于独立(单独使用乌龟)或嵌入式(乌龟和tkinter)模式,但是您将其嵌入独立模式,这可能会产生两个Tk根和其他问题。下面是嵌入模式下龟代码的修改:
from tkinter import *
from turtle import ScrolledCanvas, TurtleScreen, RawTurtle
def triangle():
turtle.forward(200)
turtle.left(135)
turtle.forward(150)
turtle.home()
def rectangle():
turtle.left(90)
for _ in range(2):
turtle.left(90)
turtle.forward(250)
turtle.left(90)
turtle.forward(125)
def square():
for _ in range(4):
turtle.forward(200)
turtle.right(90)
def circle():
turtle.circle(75)
window = Tk()
window.title('Graphics')
window.geometry('640x480+300+0')
figures = Toplevel(window)
figures.title('Figures')
figures.geometry('250x150')
Label(figures, text='Choose one figure').pack()
Button(figures, text='TRIANGLE', command=triangle).pack()
Button(figures, text='RECTANGLE', command=rectangle).pack()
Button(figures, text='SQUARE', command=square).pack()
Button(figures, text='CIRCLE', command=circle).pack()
canvas = ScrolledCanvas(window)
canvas.pack(fill="both", expand=True)
screen = TurtleScreen(canvas)
screen.screensize(640, 480)
turtle = RawTurtle(screen)
screen.mainloop()
试试这个,如果“NameError:name dot not a defined”仍然存在,请提供一个完整的错误回溯作为对原始问题的编辑。