简体   繁体   中英

Why isn't tkinter updating the percentage completion?

I am making a chess game in tkinter with an ai. Now, in my ai I have done the following (an excerpt of my total code):

global chess_board
length = len(total_moves)

for index, move in enumerate(total_moves):
    chess_board.percentage_label.config(text="Completion = {}".format(
        int((index + 1) / length) * 100))
    chess_board.update()

This is in my chessAi class and chess_board is an instance of my chessBoard class. However, tkinter waits until the end of the loop and the only two percentages displayed are 0 and 100. How can I remedy this?

Tkinter won't be able anything while you are in a function. To remedy this you can use a Thread but it would not be elegant. I would use the after method of tkinter widgets like this :

def display(index):
    move = total_moves[index]
    [your code]
    chess_board.percentage_label.after(0.01, lamda: display(index+1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM