Reputation: 41
I'm learning Tkinter by making a GUI for a sudoku solver program (that was arleady built).
On the click of a button, the algorithm that solves the sudoku runs and sometimes it takes some time.
How could I update the sudoku on the screen when the function is called, so that users can see how its running?
I'm working with a gui script separate from the sudoku one, is it correct, in design terms, to have the gui and the logic separate?
Thank in advance
EDIT
This is my code:
Sudoku.py
class Sudoku(object):
def __init__(self):
self.__matrix = [[(0, Status.Guess) for x in range(9)] for y in range(9)]
...
def solveSudoku(self):
...
GUI.py
class App:
def __init__(self, master, su):
self.__sudoku__ = su
self.__root__ = master
self.__entries__ = {}
fsudoku = Frame(master)
fsudoku.grid(row=0)
self.displaysudoku(fsudoku) """grid of entrys"""
tButton = Button(master,text="Solve", command=self.SolveAndDisplay)
...
def refreshSudokuGrid(self):
"""USED AFTER SOLVING A SUDOKU"""
for i in range(1,10):
for j in range(1,10):
val = self.__sudoku__.value(i,j)
self.__entries__[i * 10 +j].delete(0, END)
if (val!= 0):
self.__entries__[i * 10 + j].insert(0, val)
def SolveAndDisplay(self):
self.scanSudoku()
self.__sudoku__.solveSudoku()
self.refreshSudokuGrid()
...
root = Tk()
su = Sudoku()
s = App(root, su)
root.mainloop()
Upvotes: 2
Views: 2364
Reputation: 64
I guess you must be using some loop which solves the sudoku.If this is true:
The place where your function/command for your button is defined, place the following code at the beginning of the primary loop which solves the sudoku (assuming root is your tkinter window):
root.update()
As such such a method is not fully threadsafe, but should solve the problem for general cases like yours.
Keeping GUI and logic separate is the best practice.
Upvotes: 1