from turtle import *
from Tkinter import *
class TurtleApp:
def left(self):
self.turtle.right(90)
def forward(self):
self.turtle.forward(self.step)
def right(self):
self.turtle.right(90)
def backward(self):
self.turtle.backward(self.step)
def __init__(self):
self.main = Tk()
self.main.title('TurtleApp')
self.step = 10
self.turtle = Turtle()
self.window = Screen()
self.window.setup(500,500)
self.turtle.speed(self.step)
topFrame = Frame(self.main)
topFrame.pack(side='top')
topLabel = Label(topFrame, text='Turtle Controls')
topLabel.pack()
self.createDirectionPad()
self.createStepEntry()
def createDirectionPad(self):
leftFrame = Frame(self.main)
leftFrame.pack(side='left')
leftButton = Button(leftFrame, text='Left',command=left)
forwardButton = Button(leftFrame, text='Forward',command=forward)
rightButton = Button(leftFrame, text='Right',command=right)
backwardButton = Button(leftFrame, text='Backward',command=backward)
leftButton.grid(row=1,column=0)
forwardButton.grid(row=0,column=1)
rightButton.grid(row=1,column=2)
backwardButton.grid(row=2,column=1)
def createStepEntry(self):
rightFrame = Frame(self.main)
rightFrame.pack(side='right')
stepLabel = Label(rightFrame,text='Enter step size')
stepLabel.pack(side='top')
stepEntry = Entry(rightFrame)
stepEntry.pack()
stepButton = Button(rightFrame,text='Change step size')
stepButton.pack(side='bottom')
root = TurtleApp()
root.main.mainloop()
当我运行它然后点击"转发"按钮,我得到一个类型错误,说0参数给出。除了自我(已经给出了?)我不知道该传递给它的是什么。我在这里缺少什么东西,或者它的计划如此糟糕以至于无法移动乌龟?
答案 0 :(得分:1)
您需要引用forward()
类中定义的TurtleApp
方法,而不是turtle.forward()
模块中定义的turtle
函数。后者被调用是因为from turtle import *
将forward()
放入全局范围,因此调用了错误的forward()
。
在引用您班级的方法时,将createDirectionPad()
中的代码更改为使用self
:
leftButton = Button(leftFrame, text='Left',command=self.left)
forwardButton = Button(leftFrame, text='Forward',command=self.forward)
rightButton = Button(leftFrame, text='Right',command=self.right)
backwardButton = Button(leftFrame, text='Backward',command=self.backward)