![](/img/trans.png)
[英]TypeError: test_custom_function.<locals>.<lambda>() missing 1 required positional argument: '_'
[英]How do I resolve this? TypeError: main.<locals>.<lambda>() missing 1 required positional argument: 'gamepanel'
I'm using tkinter to make 2048 game gui, I created this function control_game
I want to have it such that anytime I click these keys (up, down, left, right) on my keyboard the control_game
function should be called from my main function . 但是每當我運行代碼並按下任何這些鍵時,都會顯示此錯誤: TypeError: main.<locals>.<lambda>() missing 1 required positional argument: 'gamepanel'
我已從此鏈接中閱讀了有關綁定的信息
主function
def main(gamepanel):
won = False
lost = False
get_random(gamepanel)
get_random(gamepanel)
gamepanel.update_board()
while not (won or lost):
gamepanel.root.bind_all("<Key>", lambda event, gamepanel: control_game(event, gamepanel))
gamepanel.root.focus_set()
gamepanel.root.mainloop()
控制游戲function
def control_game(event, gamepanel):
key_pressed = event.keysym
if key_pressed == "Up": # Up
transpose(gamepanel)
compress(gamepanel)
merge(gamepanel)
compress(gamepanel)
transpose(gamepanel)
elif key_pressed == "Down": #Down
transpose(gamepanel)
reverse(gamepanel)
compress(gamepanel)
merge(gamepanel)
compress(gamepanel)
reverse(gamepanel)
transpose(gamepanel)
elif key_pressed == "Left": #Left
compress(gamepanel)
merge(gamepanel)
compress(gamepanel)
elif key_pressed == "Right": # Right
reverse(gamepanel)
compress(gamepanel)
merge(gamepanel)
compress(gamepanel)
reverse(gamepanel)
這是我的板 class:
class Board(Frame):
def __init__(self):
self.FOREGROUND_COLOR = {
2: "#776e65",
4: "#776e65",
8: "#f9f6f2",
16: "#f9f6f2",
32: "#f9f6f2",
64: "#f9f6f2",
128: "#f9f6f2",
256: "#f9f6f2",
512: "#f9f6f2",
1024: "#f9f6f2",
2048: "#f9f6f2",
4096: "#776e65",
8192: "#f9f6f2",
16384: "#776e65",
32768: "#776e65",
65536: "#f9f6f2",
}
self.BACKGROUND_COLOR = {
2: "#eee4da",
4: "#ede0c8",
8: "#f2b179",
16: "#f59563",
32: "#f67c5f",
64: "#f65e3b",
128: "#edcf72",
256: "#edcc61",
512: "#edc850",
1024: "#edc53f",
2048: "#edc22e",
4096: "#eee4da",
8192: "#edc22e",
16384: "#f2b179",
32768: "#f59563",
65536: "#f67c5f",
}
self.boardSize = 4
self.moved = False
self.merged = False
self.compressed = False
self.gameMatrix = []
self.gameTile = [self.boardSize * [0] for _ in range(self.boardSize)]
self.score = 0
self.root = Tk()
self.root.title("2048 by denstream-io")
self.windowStyle = ttk.Style()
self.windowStyle.configure(
"Grid.TFrame",
background="#92877D", # BBADAO
boderwidth=5,
relief="raised",
width="500p",
height="500p",
)
self.cellStyle = ttk.Style()
self.cellStyle.configure(
"Cell.TFrame",
# background="yellow",#EEE4DA
boderwidth=5,
)
self.labelStyle = ttk.Style()
self.labelStyle.configure(
"Label.TLabel",
anchor="center",
)
self.gameWindow = ttk.Frame(self.root, style="Grid.TFrame")
self.gameWindow.grid(sticky=(N, W, E, S))
for i in range(int(self.boardSize)):
labeled_row = []
for j in range(int(self.boardSize)):
self.gameCell = ttk.Frame(
self.gameWindow, relief="raised", style="Cell.TFrame"
)
self.gameCell.grid(column=i, row=j, sticky=(N, W, E, S))
self.eachLabel = ttk.Label(
self.gameWindow, text="", style="Label.TLabel"
)
self.eachLabel.grid(column=i, row=j, sticky=(N, W, E, S))
labeled_row.append(self.eachLabel)
self.gameMatrix.append(labeled_row)
for child in self.gameWindow.winfo_children():
child.grid_configure(padx=5, pady=5, ipadx="25p", ipady="25p")
def update_board(self):
for i in range(self.boardSize):
for j in range(self.boardSize):
if self.gameTile[i][j] == 0:
self.gameMatrix[i][j].configure(
text="", background="#9E948A"
)
else:
self.gameMatrix[i][j].configure(
text=str(self.gameTile[i][j]),
background=self.BACKGROUND_COLOR[self.gameTile[i][j]],
foreground=self.FOREGROUND_COLOR[self.gameTile[i][j]],
)
整個追溯:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\dennis-lc\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
TypeError: main.<locals>.<lambda>() missing 1 required positional argument: 'gamepanel'
每當我退出游戲框架時,都會顯示此錯誤:
Traceback (most recent call last):
File "C:\Users\dennis-lc\Desktop\denstream-io\games\2048\2048\project.py", line 363, in <module>
main(gamepanel)
File "C:\Users\dennis-lc\Desktop\denstream-io\games\2048\2048\project.py", line 143, in main
gamepanel.root.bind_all("<Key>", lambda event, gamepanel: control_game(event, gamepanel))
File "C:\Users\dennis-lc\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1435, in bind_all
return self._bind(('bind', 'all'), sequence, func, add, 0)
File "C:\Users\dennis-lc\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1375, in _bind
self.tk.call(what + (sequence, cmd))
_tkinter.TclError: can't invoke "bind" command: application has been destroyed
我在文件末尾的代碼:
if __name__ == '__main__':
gamepanel = Board()
main(gamepanel)
當觸發事件回調時,tkinter 會傳遞一個包含事件信息的位置參數。
這是您的 lambda 定義:
lambda event, gamepanel: control_game(event, gamepanel))
大致相當於這樣:
def something(event, gamepanel):
control_game(event, gamepanel)
注意它需要兩個參數: event
和gamepanel
。 由於 tkinter 僅傳遞一個參數,因此您會收到錯誤missing 1 required positional argument: 'gamepanel'
,它准確地描述了問題。
解決方案是將位置參數轉換為具有默認值的關鍵字參數:
lambda event, gamepanel=gamepanel: control_game(event, gamepanel))
# ^^^^^^^^^^
Tkinter 將傳遞單個位置參數event
,而另一個參數將被賦予默認值。
錯誤can't invoke "bind" command: application has been destroyed
再次准確描述了問題。 考慮這段代碼:
while not (won or lost):
gamepanel.root.bind_all("<Key>", lambda event, gamepanel: control_game(event, gamepanel))
gamepanel.root.focus_set()
gamepanel.root.mainloop()
當 window 被破壞時, gamepanel.root.mainloop()
將退出。 當它退出時, while
循環將 go 進行第二次迭代。 在該迭代中,您嘗試對剛剛被破壞的 window 進行綁定。 因此, can't invoke "bind" command: application has been destroyed
。
解決方案是移除循環。 幾乎沒有理由在循環中調用mainloop
。 如果您覺得需要,那么您需要確保每次迭代都重新創建 window。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.