Search code examples
pythontkintertk-toolkittic-tac-toe

Tkinter dose not update the gui intermediately, unless it finishes processing rest


I am new at programming and python, thinter please find example code, it's a tic tack toe game, I want the gui to update my 'marking x' frame before it starts processing next frame.

i do not know how to make it work,

try:
    import tkinter as tk
except:
    import Tkinter as tk
import time as t
import random as r

data_list = ['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9']
used_list = ['a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9']
color = ['green yellow', 'yellow', 'green yellow', 'yellow',
         'green yellow', 'yellow', 'green yellow', 'yellow', 'green yellow']
row_col = [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)]
unused = list

mw = tk.Tk()
mw.geometry("300x300+50+50")

for i in range(3):
    mw.columnconfigure(i, weight=10)
    mw.rowconfigure(i, weight=10)

for j in range(9):
    data_list[j] = tk.Frame(mw)
    data_list[j].grid(row=row_col[j][0], column=row_col[j][1])
    data_list[j].config(background=color[j], width=100, height=100)
    data_list[j].rowconfigure(0, minsize=100)
    data_list[j].columnconfigure(0, minsize=100)
    tk.Button(data_list[j], background=color[j], border=1, command=lambda x=j: cross_mark(data_list[x])).grid(row=0, column=0, columnspan=1, rowspan=1, sticky='nsew')


def cross_mark(blk):
    tk.Button(blk, background='orange red', border=4, text='X', font='30').grid(row=0, column=0, columnspan=1, rowspan=1, sticky='nsew')
    comp_mark(blk)


def comp_mark(used):
    blk = r.choice(data_list)
    if blk == used:
        while blk == used:
            blk = r.choice(data_list)
    t.sleep(5)
    tk.Button(blk, background='RoyalBlue1', border=4, text='X', font='30').grid(row=0, column=0, columnspan=1, rowspan=1, sticky='nsew')
mw.mainloop()

plz run the above code and see if you can fix my problem


Solution

  • Use the after method provided by tkinter to process a certain function/callback event after a certain amount of time. It is never feasible to use time.sleep when using tkinter, as tkinter itself is in a constant wait state (i.e., it is waiting for button clicks or other events)

    Add the following in your cross_mark function:

    def cross_mark(blk):
        tk.Button(blk, background='orange red', border=4, text='X', font='30').grid(row=0, column=0, columnspan=1, rowspan=1, sticky='nsew')
        mw.update_idletasks()
        mw.after(1000, comp_mark(blk))