[英]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. 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 . But anytime I run the code and press any of those keys this error is displayed: TypeError: main.<locals>.<lambda>() missing 1 required positional argument: 'gamepanel'
但是每当我运行代码并按下任何这些键时,都会显示此错误:
TypeError: main.<locals>.<lambda>() missing 1 required positional argument: 'gamepanel'
I have read about binding from this link我已从此链接中阅读了有关绑定的信息
Main function主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()
Control game function控制游戏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)
This my board class:这是我的板 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]],
)
Entire Traceback:整个追溯:
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'
Anytime I exit the game frame, this error is displayed:每当我退出游戏框架时,都会显示此错误:
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
My code at the end of the file:我在文件末尾的代码:
if __name__ == '__main__':
gamepanel = Board()
main(gamepanel)
When an event callback is triggered, tkinter passes one positional argument containing information about the event.当触发事件回调时,tkinter 会传递一个包含事件信息的位置参数。
This is your lambda definition:这是您的 lambda 定义:
lambda event, gamepanel: control_game(event, gamepanel))
It is roughly equivalent to this:大致相当于这样:
def something(event, gamepanel):
control_game(event, gamepanel)
Notice how it requires two parmeters: event
and gamepanel
.注意它需要两个参数:
event
和gamepanel
。 Since tkinter only passes a single parameter, you get the error missing 1 required positional argument: 'gamepanel'
which accurately describes the problem.由于 tkinter 仅传递一个参数,因此您会收到错误
missing 1 required positional argument: 'gamepanel'
,它准确地描述了问题。
The solution is to convert the positional argument to a keyword argument with a default value:解决方案是将位置参数转换为具有默认值的关键字参数:
lambda event, gamepanel=gamepanel: control_game(event, gamepanel))
# ^^^^^^^^^^
Tkinter will pass a single positional parameter, event
, and the other argument will be given the default value. Tkinter 将传递单个位置参数
event
,而另一个参数将被赋予默认值。
The error can't invoke "bind" command: application has been destroyed
is again accurately describing the problem.错误
can't invoke "bind" command: application has been destroyed
再次准确描述了问题。 Consider this code:考虑这段代码:
while not (won or lost):
gamepanel.root.bind_all("<Key>", lambda event, gamepanel: control_game(event, gamepanel))
gamepanel.root.focus_set()
gamepanel.root.mainloop()
When the window is destroyed, gamepanel.root.mainloop()
will exit.当 window 被破坏时,
gamepanel.root.mainloop()
将退出。 When it exits, the while
loop will go to a second iteration.当它退出时,
while
循环将 go 进行第二次迭代。 In that iteration you're trying to do a binding on the window that was just destroyed.在该迭代中,您尝试对刚刚被破坏的 window 进行绑定。 Hence,
can't invoke "bind" command: application has been destroyed
.因此,
can't invoke "bind" command: application has been destroyed
。
The solution is to remove the loop.解决方案是移除循环。 There is almost never a reason to call
mainloop
in a loop.几乎没有理由在循环中调用
mainloop
。 If you feel you need to, then you need to make sure that each iteration recreates the window.如果您觉得需要,那么您需要确保每次迭代都重新创建 window。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.