[英]Python Tkinter after() does not work as expected
I am implementing the Reversi/Othello board game at Python using Tkinter for the GUI. 我正在使用Tkinter的GUI在Python上实现Reversi / Othello棋盘游戏。 The issue is that when a player makes a move I want each piece to be drawn with a time interval.
问题是,当玩家移动时,我希望每隔一段时间绘制一次。 So if 3 discs are to be drawn, I want the first disc to be drawn, then the second one after x ms , and so forth.
因此,如果要绘制3张光盘,我希望绘制第一张光盘,然后绘制x ms之后的第二张光盘,依此类推。
def draw_game(self,delay=True):
# indexes of pieces on the board
idxs = np.where(self._gameImage >0);
for (row,column) in zip(idxs[0],idxs[1]):
# Convert row,column to canvas coordinates
x1,y1,x2,y2 = self.convert_idx(col=column-1,row=row-1)
if delay:
#draw pieces with 500ms delay in between
self.after(500,self.draw_piece,row,column)
else:
self.draw_piece(row,column)
# function that draws a single piece(disc) on the board
def draw_piece(self,row,column):
x1,y1,x2,y2 = self.convert_idx(col=column-1,row=row-1)
self.canvas.create_oval(x1,y1,x2,y2,fill=self.number_to_color(self._gameImage[row,column]))
So one would expect that each piece will be drawn with 500ms time interval between the current and the next piece. 因此,人们希望每一张画在当前和下一张之间的间隔为500毫秒。 Instead, when I run the code, there is an initial time interval of 500ms and then all the pieces are drawn at once.
相反,当我运行代码时,初始时间间隔为500ms,然后立即绘制所有片段。 I tried to run with -u but the behavior was the same.
我尝试使用-u运行,但是行为是相同的。 Any help?
有什么帮助吗? This should be fairly simple
这应该很简单
I think your the placement of your: 我认为您的位置是:
self.after(500,self.draw_piece,row,column)
Needs to be moved to the end of your draw_piece() function. 需要移到draw_piece()函数的末尾。 Here's another question that demonstrates the placement.
这是另一个说明展示位置的问题。 Tkinter understanding after() It should call back to the function that its being used in.
Tkinter理解after()它应该调用其所使用的函数。
# function that draws a single piece(disc) on the board
def draw_piece(self,row,column):
x1,y1,x2,y2 = self.convert_idx(col=column-1,row=row-1)
self.canvas.create_oval(x1,y1,x2,y2,fill=self.number_to_color(self._gameImage[row,column]))
self.after(500,self.draw_piece,row,column)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.