May-22-2020, 04:29 PM
Hello,
I've got coding experience but I'm new to python.
I'm trying to generate a sudoku solver in python using tkinter. I found online the code - now I'm trying to link it with tkinter. The code uses the list bo - this list contains the entered values from the entries of tkinter.
How to pass the list "bo" to the solve function when the button "readin" is pressed?
I tried " bo=readin() " (line 91).
Here is the error description:
Traceback (most recent call last):
File "d:/Ordner/Python/testgrid2/testgrid2.py", line 91, in <module>
bo=readin()
File "d:/Ordner/Python/testgrid2/testgrid2.py", line 70, in readin
listrow0.append(Square[i].get())
IndexError: list index out of range
What I am doing wrong?
Thanks for your help!!
Greetings,
flash77
I've got coding experience but I'm new to python.
I'm trying to generate a sudoku solver in python using tkinter. I found online the code - now I'm trying to link it with tkinter. The code uses the list bo - this list contains the entered values from the entries of tkinter.
How to pass the list "bo" to the solve function when the button "readin" is pressed?
I tried " bo=readin() " (line 91).
Here is the error description:
Traceback (most recent call last):
File "d:/Ordner/Python/testgrid2/testgrid2.py", line 91, in <module>
bo=readin()
File "d:/Ordner/Python/testgrid2/testgrid2.py", line 70, in readin
listrow0.append(Square[i].get())
IndexError: list index out of range
What I am doing wrong?
Thanks for your help!!
Greetings,
flash77
import tkinter as tk from tkinter import Entry, IntVar, Tk 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 def quit_frame(): main.destroy() Square = [] 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)) t.place(x=i*40+70, y=j*40+80, width=40, height=40) t.delete(0) Square.append(data) def readin(): listrow0 = []; listrow1 = []; listrow2 = []; listrow3 = []; listrow4 = []; listrow5 = []; listrow6 = [] listrow7 = []; listrow8 = [] for i in range(0,9): listrow0.append(Square[i].get()) for i in range(9,18): listrow1.append(Square[i].get()) for i in range(18,27): listrow2.append(Square[i].get()) for i in range(27,36): listrow3.append(Square[i].get()) for i in range(36,45): listrow4.append(Square[i].get()) for i in range(45,54): listrow5.append(Square[i].get()) for i in range(54,63): listrow6.append(Square[i].get()) for i in range(63,72): listrow7.append(Square[i].get()) for i in range(72,81): listrow8.append(Square[i].get()) bo = [] bo = [listrow0,listrow1,listrow2,listrow3,listrow4,listrow5,listrow6,listrow7,listrow8] return bo bo=readin() #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) SquareCreate() 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) solve(bo) main.mainloop()