OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Tkinter updating window while calculating otherthings

  • Thread starter Thread starter abizittin
  • Start date Start date
A

abizittin

Guest
I am triying to write sudoku solver. This is really complicated code for me. I want to update board while python calculating other things. However, code could not do that. Should I try threading or is there easy way to do that?

CURRENT SITUATION: end of the calculation. I am inserting values. Then, I click solve. I am changing text of label (via code), but label waits untill end of calculations and suddenly applies labels' changes.

related codes in "def solve() > def set_text(), def check()"

my complete code:

Code:
import tkinter
from copy import deepcopy
import time
import threading

window = tkinter.Tk()
window.title("Sudoku Solver")
window.config(padx=30, pady=30)

zerolist = []  # adı posibles olacak
entrylist = []
exactvals = []
labellist = []

def create000():
    global zerolist
    global exactvals
    for i in range(9):
        zerolistemp = []
        exectemp = []
        for j in range(9):
            zerolistemp.append([])
            exectemp.append(0)
        zerolist.append(deepcopy(zerolistemp))
        exactvals.append(deepcopy(exectemp))
        zerolistemp.clear()
        exectemp.clear()
    return zerolist


# def solve():
#     def check(i,j):
#         global zerolist
#         for i in range(9):
#             for j in range(9):
#                 for element in zerolist[i][j]:
#                     if j == 9:


def solve():
    def set_text(x, y, text):
        time.sleep(1)
        global labellist
        if str(text) == "":
            labellist[x][y].config(text=str(text), background="gray")
        elif len(zerolist[x][y]) == 1:
            labellist[x][y].config(text=str(text), background="lightgreen")
        elif text != 0:
            labellist[x][y].config(text=str(text), background="red")
        return

    def check(i, j):
        print("check")
        global zerolist
        global exactvals
        columnpart = int(i / 3)
        rowpart = int(j / 3)
        for element in zerolist[i][j]:
            print(element)
            set_text(i, j, element)
            time.sleep(0.5)
            # elementi lable a yaz kırmızı yap
            # 3x3
            for m in range(3):
                for n in range(3):
                    if element == exactvals[columnpart + m][rowpart + n]:
                        zerolist[i][j].pop(element)
                        set_text(i, j, "")
                        return False

            if element in exactvals[i] or element in [row[j] for row in exactvals]:
                zerolist[i][j].pop(element)
                set_text(i, j, "")
                return False

            exactvals[i][j] = element
            if j != 8:
                if check(i, j + 1) == False:
                    exactvals[i][j] = 0
                    zerolist[i][j].pop(element)
                    set_text(i, j, "")
                    return False
            elif i != 8:
                if check(i + 1, 0) == False:
                    exactvals[i][j] = 0
                    zerolist[i][j].pop(element)
                    set_text(i, j, "")
                    return False
            else:
                # write yeşil
                set_text(i, j, element)
                return True

    check(0, 0)
    return exactvals


def collect_data():  # collec ederken zaten var  i j kullanıp uctan ekleyebiliriz
    global zerolist
    global exactvals
    global labellist
    for i in range(9):
        for j in range(9):  # yatay liste değiştiriyor
            if len(entrylist[i][j].get()) != 0:
                value = [int(entrylist[i][j].get())]
                exc = int(entrylist[i][j].get())
                labellist[i][j].config(text=exc, background="lightgreen")
            else:
                value = [x + 1 for x in range(9)]
                exc = 0
            zerolist[i][j] = value
            exactvals[i][j] = exc
    solve()
    return zerolist

for row in range(9):
    tempentry = []
    for column in range(9):
        a = tkinter.Entry(width=3)
        a.grid(column=column, row=row)
        tempentry.append(a)
    entrylist.append(tempentry)

emptylabel = tkinter.Label(width=3)
emptylabel.grid(column=9,row=0,rowspan=9)

for row in range(9):
    templabel = []
    for column in range(10, 19):
        a = tkinter.Label(width=3,borderwidth=2, relief="groove")
        a.grid(column=column, row=row)
        templabel.append(a)
    labellist.append(templabel)


but_solve = tkinter.Button(command=collect_data, text="SOLVE", width=9, pady=5)
but_solve.grid(columnspan=3, row=9, column=3)

create000()

window.mainloop()

I want to see changes in real-time while python calculating other things.

I tried threading like this: [![threading code][1]][1]

[1]: https://i.sstatic.net/ffyYu36t.png but tkinter does not give answer. ie stops.
<p>I am triying to write sudoku solver. This is really complicated code for me. I want to update board while python calculating other things. However, code could not do that. Should I try threading or is there easy way to do that?</p>
<p>CURRENT SITUATION: <a href="https://i.sstatic.net/62hXZiBM.png" rel="nofollow noreferrer">end of the calculation.</a>
I am inserting values. Then, I click solve. I am changing text of label (via code), but label waits untill end of calculations and suddenly applies labels' changes.</p>
<p>related codes in "def solve() > def set_text(), def check()"</p>
<p>my complete code:</p>
<pre><code>import tkinter
from copy import deepcopy
import time
import threading

window = tkinter.Tk()
window.title("Sudoku Solver")
window.config(padx=30, pady=30)

zerolist = [] # adı posibles olacak
entrylist = []
exactvals = []
labellist = []

def create000():
global zerolist
global exactvals
for i in range(9):
zerolistemp = []
exectemp = []
for j in range(9):
zerolistemp.append([])
exectemp.append(0)
zerolist.append(deepcopy(zerolistemp))
exactvals.append(deepcopy(exectemp))
zerolistemp.clear()
exectemp.clear()
return zerolist


# def solve():
# def check(i,j):
# global zerolist
# for i in range(9):
# for j in range(9):
# for element in zerolist[j]:
# if j == 9:


def solve():
def set_text(x, y, text):
time.sleep(1)
global labellist
if str(text) == "":
labellist[x][y].config(text=str(text), background="gray")
elif len(zerolist[x][y]) == 1:
labellist[x][y].config(text=str(text), background="lightgreen")
elif text != 0:
labellist[x][y].config(text=str(text), background="red")
return

def check(i, j):
print("check")
global zerolist
global exactvals
columnpart = int(i / 3)
rowpart = int(j / 3)
for element in zerolist[j]:
print(element)
set_text(i, j, element)
time.sleep(0.5)
# elementi lable a yaz kırmızı yap
# 3x3
for m in range(3):
for n in range(3):
if element == exactvals[columnpart + m][rowpart + n]:
zerolist[j].pop(element)
set_text(i, j, "")
return False

if element in exactvals or element in [row[j] for row in exactvals]:
zerolist[j].pop(element)
set_text(i, j, "")
return False

exactvals[j] = element
if j != 8:
if check(i, j + 1) == False:
exactvals[j] = 0
zerolist[j].pop(element)
set_text(i, j, "")
return False
elif i != 8:
if check(i + 1, 0) == False:
exactvals[j] = 0
zerolist[j].pop(element)
set_text(i, j, "")
return False
else:
# write yeşil
set_text(i, j, element)
return True

check(0, 0)
return exactvals


def collect_data(): # collec ederken zaten var i j kullanıp uctan ekleyebiliriz
global zerolist
global exactvals
global labellist
for i in range(9):
for j in range(9): # yatay liste değiştiriyor
if len(entrylist[j].get()) != 0:
value = [int(entrylist[j].get())]
exc = int(entrylist[j].get())
labellist[j].config(text=exc, background="lightgreen")
else:
value = [x + 1 for x in range(9)]
exc = 0
zerolist[j] = value
exactvals[j] = exc
solve()
return zerolist

for row in range(9):
tempentry = []
for column in range(9):
a = tkinter.Entry(width=3)
a.grid(column=column, row=row)
tempentry.append(a)
entrylist.append(tempentry)

emptylabel = tkinter.Label(width=3)
emptylabel.grid(column=9,row=0,rowspan=9)

for row in range(9):
templabel = []
for column in range(10, 19):
a = tkinter.Label(width=3,borderwidth=2, relief="groove")
a.grid(column=column, row=row)
templabel.append(a)
labellist.append(templabel)


but_solve = tkinter.Button(command=collect_data, text="SOLVE", width=9, pady=5)
but_solve.grid(columnspan=3, row=9, column=3)

create000()

window.mainloop()

</code></pre>
<p>I want to see changes in real-time while python calculating other things.</p>
<p>I tried threading like this: [![threading code][1]][1]</p>
<p>[1]: <a href="https://i.sstatic.net/ffyYu36t.png" rel="nofollow noreferrer">https://i.sstatic.net/ffyYu36t.png</a> but tkinter does not give answer. ie stops.</p>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top