簡體   English   中英

如何通過tkinter綁定傳遞更多的arguments

[英]How to pass more arguments through tkinter bind

如何通過 tkinter 的 bind 方法傳遞更多的 arguments?
例如:

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)

當我運行第一個時,它說事件未定義

您始終可以將回調 function 包裝在 lambda 中。

tk.bind('<Key>', lambda event: moveShip(event, 'place'))

另一個選項是使用functool中的partial來創建一個 function 並設置默認值key

from functools import partial

moveShip_place = partial(moveShip, key='place')

tk.bind('<Key>', moveShip_place)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM