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

thanks for your patience!

The Squarecreate function creates the entries.

The next steps would be:

When the given values are entered in the entries of the frame, the values shall be read in when the button readin is pressed and the function solve shall be run. When the function solve has solved the puzzle the values of the list board have to be written in the entries.

I have coding experience but I'm a beginner to python so I have to ask again and again...

Please would you be so kind and solve these steps?

Then I would have a working code I could orientate myself...

That would be perhpas a better way instead of asking and asking (and perhaps annoying) again?

Greetings

flash77

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

def quit_frame():
    main.destroy()

def SquareCreate(): 
    sq = []
    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)
            sq.append(data)
    return sq

def readin(sq):
    bo = []
    for r in range(0,9):
        row = []
        for c in range(0,9):
            row.append(sq[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
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()
Square=SquareCreate()
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