画布未更新

时间:2019-01-27 17:53:16

标签: python python-3.x tkinter tkinter-canvas

我正在尝试制作下象棋的算法。它列出了一定深度下所有可能的移动。我试图一步一步地画出可能的场景以进行测试。我使用递归函数cr_list来以深度优先的方式计算移动量。选择移动后,我更改了所涉及的各个部分的位置,并调用了draw函数来查看差异,但是画布不会更新。我使用after方法的方式似乎不正确。我也尝试过使用self.canvas.update_idletasks()方法,但是它只能运行一次,然后不再停止更改。使用ctrl + c停止程序更新后,我还将在draw函数中打印所有块的位置,它们应该是它们的位置。我使用x = input()语句停止程序以查看程序变化。

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()
    # .....

1 个答案:

答案 0 :(得分:0)

最后,我正常地调用了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)
    # .....