Python Forum
[Tkinter] tkinter freezes by clicking button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] tkinter freezes by clicking button
#7
I made it work by using the active state recipe, which I saved verbatim as a file tkcallasync.py. Then I changed your code a little in a file solvesudo.py
# solvesudo.py
sudoku = [[' ',' ',' ','2','1',' ',' ',' ',' '],
          [' ',' ','7','3',' ',' ',' ',' ',' '],
          [' ','5','8',' ',' ',' ',' ',' ',' '],
          ['4','3',' ',' ',' ',' ',' ',' ',' '],
          ['2',' ',' ',' ',' ',' ',' ',' ','8'],
          [' ',' ',' ',' ',' ',' ',' ','7','6'],
          [' ',' ',' ',' ',' ',' ','2','5',' '],
          [' ',' ',' ',' ',' ','7','3',' ',' '],
          [' ',' ',' ',' ','9','8',' ',' ',' ']]
 
def printSUDOKU(matrix):
    for row in range(9):
        print("|-----------------------------------|")
        for col in range(9):
            print("| " + matrix[row][col] + " ",end='')
        print("|")
    print("|-----------------------------------|")
 
def consistent(matrix, row, col, value):
    for i in range(9):
        if matrix[row][i]==value: return False
        if matrix[i][col]==value: return False
    rowStart = row - row%3
    colStart = col - col%3
    for m in range(3):
        for k in range(3):
            if matrix[rowStart+k][colStart+m]==value: return False
    return True

class SolutionFound(Exception):
    pass
 
def wsolve(sudoku):
    try:
        solve(sudoku, 0)
    except SolutionFound:
        return True
    return False

def solve(sudoku, num):
    if num==81:
        raise SolutionFound
        return True
    else:
        row = int(num / 9)
        col = num % 9
        if sudoku[row][col]!=' ':
            solve(sudoku, num+1)
        else:
            for value in range(1,10):
                if consistent(sudoku, row, col, str(value)):
                    sudoku[row][col] = str(value)
                    if solve(sudoku, num+1): return True
                sudoku[row][col]=' '
            return False
 
if __name__ == '__main__':
    printSUDOKU(sudoku)
    solve(sudoku, 0)
Finally, I wrote a small tkinter code, tksudo.py, similar to the example in the active state recipe
# tksudo.py
from tkinter import Tk, Frame, Entry, Label, Button, IntVar, StringVar, LEFT
from tkinter import messagebox
from tkcallasync import tk_call_async, MULTIPROCESSING
import solvesudo

def do_solve(sudoku):
    status = solvesudo.wsolve(sudoku)
    return status, sudoku

disabled = False

def execute_solver():
    global disabled
    if disabled:
        messagebox.showinfo("warning", "It's still calculating...")
        return

    def callback(result):
        global disabled
        disabled = False
        status, sudoku = result
        solvesudo.printSUDOKU(sudoku)
        result_var.set('Found!' if status else 'Not Found!')

    disabled = True
    tk_call_async(root, do_solve, args=(solvesudo.sudoku,), callback=callback, method=MULTIPROCESSING)

root = Tk()

row = Frame(root)
row.pack()
Button(row, text="Solve Sudoku", command =execute_solver).pack(side=LEFT)
Button(row, text="It's responsive", command= lambda: messagebox.showinfo("info", "it's responsive")).pack(side=LEFT)

result_var = StringVar()
Label(root, textvariable=result_var).pack()

root.mainloop()
The result is that it works very well (in linux), and the small GUI remains responsive while the peer process computes the solution.
Output:
λ python3 tksudo.py |-----------------------------------| | 6 | 4 | 3 | 2 | 1 | 5 | 8 | 9 | 7 | |-----------------------------------| | 1 | 2 | 7 | 3 | 8 | 9 | 6 | 4 | 5 | |-----------------------------------| | 9 | 5 | 8 | 7 | 6 | 4 | 1 | 2 | 3 | |-----------------------------------| | 4 | 3 | 5 | 8 | 7 | 6 | 9 | 1 | 2 | |-----------------------------------| | 2 | 7 | 6 | 9 | 5 | 1 | 4 | 3 | 8 | |-----------------------------------| | 8 | 9 | 1 | 4 | 3 | 2 | 5 | 7 | 6 | |-----------------------------------| | 7 | 8 | 9 | 6 | 4 | 3 | 2 | 5 | 1 | |-----------------------------------| | 5 | 6 | 4 | 1 | 2 | 7 | 3 | 8 | 9 | |-----------------------------------| | 3 | 1 | 2 | 5 | 9 | 8 | 7 | 6 | 4 | |-----------------------------------|
Reply


Messages In This Thread
tkinter freezes by clicking button - by Zatox11 - Mar-30-2018, 11:38 AM
RE: tkinter freezes by clicking button - by Zatox11 - Mar-30-2018, 02:31 PM
RE: tkinter freezes by clicking button - by Zatox11 - Mar-30-2018, 03:32 PM
RE: tkinter freezes by clicking button - by woooee - Mar-30-2018, 07:35 PM
RE: tkinter freezes by clicking button - by Gribouillis - Mar-30-2018, 08:26 PM
RE: tkinter freezes by clicking button - by Zatox11 - Mar-31-2018, 08:46 AM
RE: tkinter freezes by clicking button - by Zatox11 - Mar-31-2018, 09:18 AM
RE: tkinter freezes by clicking button - by Zatox11 - Mar-31-2018, 10:23 AM
RE: tkinter freezes by clicking button - by Zatox11 - Mar-31-2018, 10:48 AM
RE: tkinter freezes by clicking button - by Zatox11 - Mar-31-2018, 02:15 PM
RE: tkinter freezes by clicking button - by Zatox11 - Apr-01-2018, 11:38 AM
RE: tkinter freezes by clicking button - by Zatox11 - Apr-02-2018, 12:58 PM
RE: tkinter freezes by clicking button - by Zatox11 - Apr-02-2018, 04:07 PM
RE: tkinter freezes by clicking button - by Zatox11 - Apr-03-2018, 10:50 AM
RE: tkinter freezes by clicking button - by Zatox11 - Apr-03-2018, 02:58 PM
RE: tkinter freezes by clicking button - by sylas - Apr-06-2018, 07:57 AM
RE: tkinter freezes by clicking button - by sylas - Apr-06-2018, 09:10 AM
RE: tkinter freezes by clicking button - by sylas - Apr-08-2018, 09:32 AM
RE: tkinter freezes by clicking button - by sylas - Apr-08-2018, 12:01 PM
RE: tkinter freezes by clicking button - by sylas - Apr-08-2018, 01:05 PM
RE: tkinter freezes by clicking button - by sylas - Apr-08-2018, 03:28 PM
RE: tkinter freezes by clicking button - by Zatox11 - Apr-10-2018, 09:03 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 1,537 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 1,169 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 6,194 May-25-2023, 07:37 PM
Last Post: deanhystad
  [Tkinter] Clicking on the button crashes the TK window ODOshmockenberg 1 2,379 Mar-10-2022, 05:18 PM
Last Post: deanhystad
  Can't get tkinter button to change color based on changes in data dford 4 3,648 Feb-13-2022, 01:57 PM
Last Post: dford
  Creating a function interrupt button tkinter AnotherSam 2 5,817 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,353 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter showing image in button rwahdan 3 5,895 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 4,282 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  tkinter python button position problem Nick_tkinter 3 3,760 Jan-31-2021, 05:15 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020