【发布时间】:2017-05-24 06:00:55
【问题描述】:
我在 Python3 上写国际象棋,使用 Tkinter。我对机器人模式有疑问。玩家正确播放:点击按钮和人物移动。在玩家回合后,机器人应该玩。
def main():
start_position()
root.mainloop()
while king_alive:
global bot_turn
if bot_turn:
cells = l.bot_move(log_field)
replace(cells[0], cells[1])
bot_turn = False
这是函数 replace(),它改变了字段上单元格的条件
def replace(first_cell, second_cell):
if second_cell.figure.type != 'nofig':
delete_fig(second_cell.figure)
first_cell.clicked = False
second_cell.clicked = False
second_cell.fig_name = first_cell.fig_name
second_cell.fig_owner = first_cell.fig_owner
second_cell.figure = second_cell.get_figure()
first_cell.figure = l.NoFigure(first_cell.x, first_cell.y, "")
first_cell.fig_name = ""
first_cell.fig_owner = ""
field[second_cell.x][second_cell.y].configure(fg=second_cell.fig_owner, text=second_cell.fig_name)
field[first_cell.x][first_cell.y].configure(text="")
demark_cells()
人们可以轻松地玩和移动人物,但机器人不能这样做,但是他使用相同的移动功能 - replace()(方法 replace() 的输入是正确的。我无法理解回溯
Traceback (most recent call last):
File "C:/Users/Даша/Desktop/python/task chess/graphics.py", line 176, in <module>
main()
File "C:/Users/Даша/Desktop/python/task chess/graphics.py", line 171, in main
replace(cells[0], cells[1])
File "C:/Users/Даша/Desktop/python/task chess/graphics.py", line 138, in replace
field[second_cell.x][second_cell.y]['fg'] = second_cell.fig_owner
File "C:\Python34\lib\tkinter\__init__.py", line 1275, in __setitem__
self.configure({key: value})
File "C:\Python34\lib\tkinter\__init__.py", line 1268, in configure
return self._configure('configure', cnf, kw)
File "C:\Python34\lib\tkinter\__init__.py", line 1259, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: invalid command name ".36689776"
如何解决问题? 附言log_field 是每个单元格条件的矩阵 字段是 tkinter 按钮的矩阵
【问题讨论】:
-
可能是 root.mainloop 和 while (king_alive) 的代码的配置问题...等
-
在 tkinter 中,大多数时候该错误意味着您正在尝试对您进行破坏或未创建的操作。也许您正在销毁 delete_fig 中的一个对象,然后尝试在那里对其进行操作?
-
另外,请阅读minimal reproducible example。这段代码远非完整和可验证的。
标签: python-3.x tkinter graphics artificial-intelligence