简体   繁体   English

Tkinter 无效命令名称“.!button”

[英]Tkinter invalid command name “.!button”

When I run my tkinter code, I run into this error:当我运行我的 tkinter 代码时,我遇到了这个错误:

File "/Users/owen/PycharmProjects/Tutorials/Make7.py", line 147, in user_turn
    btn.config(command=partial(self.tile_prep, btn_n))
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1637, in configure
    return self._configure('configure', cnf, kw)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/tkinter/__init__.py", line 1627, in _configure
    self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".!button"

The line that causes the error is this:导致错误的行是这样的:

btn.config(command=partial(self.tile_prep, btn_n))

I'm not sure what's causing the error.我不确定是什么导致了错误。 Originally, I used原来,我用

btn['command'] = partial(self.tile_prep, btn_n)

I tried changing to .config() , but this didn't work.我尝试更改为.config() ,但这不起作用。 I'm not sure if it's a problem with partial .我不确定这是否是partial的问题。 If so, is there another way to pass arguments into button commands?如果是这样,是否有另一种方法将 arguments 传递给按钮命令? In this case, btn_n simply points to the button that was pressed.在这种情况下, btn_n只是指向被按下的按钮。

Edit: I can't seem to reproduce the problem without including the entire code.编辑:如果不包含整个代码,我似乎无法重现该问题。 Also, the error is very weird: I'm making a connect-4 program, and the error only occurs when the middle three squares in the bottom row are filled.另外,错误很奇怪:我正在制作一个 connect-4 程序,并且仅在填充了底行中间的三个正方形时才会出现错误。 I've looked through the code, and the inner workings seem to be working right, just the GUI component.我查看了代码,内部工作似乎正常工作,只是 GUI 组件。

Edit 2: I was able to fix the issue by using .bind() instead of using the command argument.编辑 2:我能够通过使用.bind()而不是使用command参数来解决问题。 I'm still curious why this issue occurred, though.不过,我仍然很好奇为什么会出现这个问题。

You are calling multiple values in a button.So you have to intrigate it on a single function Such as您在一个按钮中调用多个值。因此您必须在单个 function 上对其进行处理,例如

btn.config(command=lambda:[self.funcA(x, y)])

Try this it may solve your problem试试这个它可能会解决你的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM