如何通过tkinter的bind方法传递更多参数?
例如:
tk = Tk()
def moveShip(event,key):
if event.keysym == 'Down' and player1.selectedCoord[1] != 9:
if key == 'place':
player1.selectedCoord[1] += 1
elif event.keysym == 'Up' and player1.selectedCoord[1] != 0:
if key == 'place':
player1.selectedCoord[1] -= 1
elif event.keysym == 'Left' and player1.selectedCoord[0] != 0:
if key == 'place':
player1.selectedCoord[0] -= 1
elif event.keysym == 'Right' and player1.selectedCoord[0] != 9:
if key == 'place':
player1.selectedCoord[0] += 1
tk.bind("<Key>", command=moveShip(event,'place'))
代替
tk.bind("<Key>", moveship)
当我运行第一个时,它表示事件未定义
答案 0 :(得分:0)
您始终可以将回调函数包装在lambda中。
tk.bind('<Key>', lambda event: moveShip(event, 'place'))
另一种选择是使用partial
中的functool
创建一个默认值key
已设置的函数。
from functools import partial
moveShip_place = partial(moveShip, key='place')
tk.bind('<Key>', moveShip_place)