python - 画布没有更新
问题描述
我正在尝试制作一种下棋的算法。它列出了一定深度的所有可能的移动。我试图在每一步之后一一绘制可能的场景以进行测试。我使用递归函数 cr_list 以深度优先的方式计算移动。选择移动后,我更改所涉及的部分的各自位置并调用绘图函数以查看差异。但画布不会更新。我使用 after 方法的方式似乎不对。我也尝试使用 self.canvas.update_idletasks() 方法,但它只工作一次,然后再次停止更改。在我使用 ctrl+c 停止它更新的程序之后,我还打印了绘图函数中所有部分的位置,它们应该是它们应该是的位置。
def draw(self,piece):
if self.flag:
self.canvas.delete('all')
self.flag=1
x=(800-(int(self.n)*int((660/int(self.n)))))/2
y=(800-(int(self.n)*int((660/int(self.n)))))/2
for i in range(int(self.n)):
if i%2==0:
for j in range(int(self.n)):
if j%2==0:
self.canvas.create_rectangle(x,y,x+int((660/int(self.n))),y+int((660/int(self.n))), fill='navajo white')
else:
self.canvas.create_rectangle(x,y,x+int((660/int(self.n))),y+int((660/int(self.n))), fill='saddle brown')
x+=int((660/int(self.n)))
else:
for j in range(int(self.n)):
if j%2==0:
self.canvas.create_rectangle(x,y,x+int((660/int(self.n))),y+int((660/int(self.n))), fill='saddle brown')
else:
self.canvas.create_rectangle(x,y,x+int((660/int(self.n))),y+int((660/int(self.n))), fill='navajo white')
x+=int((660/int(self.n)))
y+=int((660/int(self.n)))
x=(800-(int(self.n)*int((660/int(self.n)))))/2
x=(800-(int(self.n)*int((660/int(self.n)))))/2
y=(800-(int(self.n)*int((660/int(self.n)))))/2
for i in range(16):
if piece[i]!=None:
if (piece[i].pos[0]+piece[i].pos[1])%2==0:
word=piece[i].species+'_aa.ppm'
else:
word=piece[i].species+'_am.ppm'
self.img[i]=tkinter.PhotoImage(file=word)
self.canvas.create_image(x+piece[i].pos[1]*int((660/int(self.n)))+(int((660/int(self.n)))-73)/2,y+piece[i].pos[0]*int((660/int(self.n)))+(int((660/int(self.n)))-73)/2, anchor=tkinter.NW,image=self.img[i])
print(piece[i].pos)
for i in range(16):
if piece[i]!=None:
if (piece[i+16].pos[0]+piece[i+16].pos[1])%2==0:
word=piece[i+16].species+'_ma.ppm'
else:
word=piece[i+16].species+'_mm.ppm'
self.img[i+16]=tkinter.PhotoImage(file=word)
self.canvas.create_image(x+piece[i+16].pos[1]*int((660/int(self.n)))+(int((660/int(self.n)))-73)/2,y+piece[i+16].pos[0]*int((660/int(self.n)))+(int((660/int(self.n)))-73)/2, anchor=tkinter.NW,image=self.img[i+16])
def cr_list(self,i,lm,met,pieces):
# .....
root.after(1000,self.draw(pieces2))
self.cr_list(i2,lm,met,pieces2)
# .....
x=input()
# .....
解决方案
最后,我正常调用了 draw(),在下面我使用了 self.canvas.update() 并且它起作用了。
def cr_list(self,i,lm,met,pieces):
# .....
self.draw(pieces2)
self.canvas.update()
x=input()
self.cr_list(i2,lm,met,pieces2)
# .....
推荐阅读
- java - 与“for”循环相比,为什么 java 流的性能会因工作量较大而下降?
- android - android.app.AlertDialog 自定义主题不起作用
- python-3.x - 数据框行值相关性
- input - Coldfusion 输入类型 = 'hidden' 不隐藏
- java - 如何用空格打印匹配数字的结果
- php - 按 Rand wp 查询排序
- c - 为什么我的程序一直打印垃圾值?
- python - Buttonbehaviour,带有由 python 列表动态生成的按钮(kivy)
- mysql - 具有对同一个表的多个引用的 SQL 递归查询
- jms - 事务在故障转移时回滚,但提交可能已成功