Tkinter:绑定功能问题

Tkinter : Issue with binding function

我有一个项目在 canvas 上使用 python 的海龟模块,我正在尝试将 <Return> 键绑定到执行自定义命令的函数我做的。这是一个例子:

from tkinter import *
import turtle
root = Tk()


mainframe=Frame(root)
mainframe.pack(fill=BOTH)
screen = turtle.ScrolledCanvas(mainframe)
tt = turtle.RawTurtle(screen)

def scrollStart(event): #these functions are for scrolling the widget
    screen.scan_mark(event.x, event.y)
def scrollDrag(event):
    screen.scan_dragto(event.x, event.y, gain = 1)

text = Text(root)
text.pack()

def executeCommand(): #Problem here
    def moveForward(pixels):
        tt.forward(pixels)



root.bind("<Return>",executeCommand)

root.mainloop()

但是当我 运行 它并点击 moveForward(15) 时,它说:

TypeError: executeCommand() takes 0 positional arguments but 1 was given

您需要向 executeCommand() 注入一个参数。所以把它的定义改成:

def executeCommand(event): 
    def moveForward(pixels):
        tt.forward(pixels)