Python Forum
[Tkinter] Sudoku solver with tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Sudoku solver with tkinter
#3
Hello deanhystad,

thanks for your answer!!

I added in line 12 to 21 some code for spaces between the entries.

I use the acronym "bo" for "board".

Unfortunately there is an error in line 32, for the expression "bo" it is mentioned "invalid syntax".

I tried very long to find the reason for it but I came to no solution...

What could I do?

Thanks again,

Greetings flash77

import tkinter as tk
from tkinter import Entry, IntVar, Tk

def quit_frame():
    main.destroy()

def SquareCreate(): 
    for j in range(0, 9):
        for i in range(0,9):
            data = IntVar()
            t = tk.Entry(main, textvariable=data, justify="center",font=("Arial",16))
            ixtra=0
            jxtra=0
            if i > 2:
                ixtra=4
            if i > 5:
                ixtra=8
            if j > 2:
                jxtra=4
            if j > 5:
                jxtra=8
            t.place(x=i*40+70+ixtra, y=j*40+80+jxtra, width=40, height=40)
            t.delete(0)
            Square.append(data)

def readin():
    bo = []
    for r in range(9):
        row = []
        for c in range(9):
            row.append(Square[r*9+c].get()
        bo.append(row)    
    return bo 

def solve(bo):
    find = find_empty(bo)
    if not find:
        print("Sudoku solved.")
        return True
    else:
        row, col = find

    for i in range(1,10):
        if valid(bo, i, (row, col)):
            bo[row][col] = i

            if solve(bo):
                return True

            bo[row][col] = 0

    return False

def valid(bo, num, pos):
    # Check row
    for i in range(len(bo[0])):
        if bo[pos[0]][i] == num and pos[1] != i:
            return False

    # Check column
    for i in range(len(bo)):
        if bo[i][pos[1]] == num and pos[0] != i:
            return False

    # Check box
    box_x = pos[1] // 3
    box_y = pos[0] // 3

    for i in range(box_y*3, box_y*3 + 3):
        for j in range(box_x * 3, box_x*3 + 3):
            if bo[i][j] == num and (i,j) != pos:
                return False

    return True

def find_empty(bo):
    for i in range(len(bo)):
        for j in range(len(bo[0])):
            if bo[i][j] == 0:
                return (i, j)  # row, col
    
    return None

#mainprogramm
Square = []
main = tk.Tk()
main.geometry("500x540")
main.resizable(width=0, height=0)
l=tk.Label(main, text="Sudoku Bruteforce Solver")
l["font"]="Arial"
l.place(x=150,y=0)
button1=tk.Button(main, text="quit", command = quit_frame)
button1.place(x=50,y=450)
button2=tk.Button(main, text="readin", command = readin)
button2.place(x=150,y=450)
SquareCreate()
bo=readin()
print(bo)
solve(bo)

main.mainloop()
Reply


Messages In This Thread
Sudoku solver with tkinter - by flash77 - May-22-2020, 04:29 PM
RE: Sudoku solver with tkinter - by deanhystad - May-22-2020, 05:54 PM
RE: Sudoku solver with tkinter - by flash77 - May-23-2020, 07:06 PM
RE: Sudoku solver with tkinter - by deanhystad - May-24-2020, 03:30 AM
RE: Sudoku solver with tkinter - by flash77 - May-24-2020, 03:41 PM
RE: Sudoku solver with tkinter - by flash77 - May-27-2020, 06:40 AM
RE: Sudoku solver with tkinter - by deanhystad - May-28-2020, 04:30 AM
RE: Sudoku solver with tkinter - by flash77 - May-28-2020, 02:32 PM
RE: Sudoku solver with tkinter - by deanhystad - May-28-2020, 02:45 PM

Forum Jump:

User Panel Messages

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