[英]Python Tkinter after() does not work as expected
我正在使用Tkinter的GUI在Python上實現Reversi / Othello棋盤游戲。 問題是,當玩家移動時,我希望每隔一段時間繪制一次。 因此,如果要繪制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]))
因此,人們希望每一張畫在當前和下一張之間的間隔為500毫秒。 相反,當我運行代碼時,初始時間間隔為500ms,然后立即繪制所有片段。 我嘗試使用-u運行,但是行為是相同的。 有什么幫助嗎? 這應該很簡單
我認為您的位置是:
self.after(500,self.draw_piece,row,column)
需要移到draw_piece()函數的末尾。 這是另一個說明展示位置的問題。 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.