This repository was archived by the owner on Jan 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPython Sudoku GUI.py
143 lines (121 loc) · 4.59 KB
/
Python Sudoku GUI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from tkinter import *
from tkinter import ttk
from Sudoku_Checker import solved
class Sudoku(ttk.Frame):
def __init__(self, master):
super().__init__(master, borderwidth = 5)
self.pack()
temp = [9]*9
for row in problem:
for col in row:
if col != -1 and col != 0:
temp[col-1] -= 1
self.numbersLeft = temp
print(self.numbersLeft)
self.create_widgets()
def create_widgets(self):
for vr in range(9):
for hr in range(9):
if problem[vr][hr] != 0:
if (vr//3+hr//3)%2 == 0:
label = ttk.Label(self, text = problem[vr][hr], width = 4, relief = "groove") #flat, groove, raised, ridge, solid, or sunken
else:
label = ttk.Label(self, text = problem[vr][hr], width = 4, relief = "groove", background = "#bdbdbd")
label.grid(column = hr, row = vr, sticky = (N,W,E,S))
label.configure(anchor = "center")
else:
button = ttk.Button(self, width = 4)
button.bind("<Button-1>", self.selectBtn)
button.grid(column = hr, row = vr, sticky = (N, W, E, S))
col = 9
for h in range(9):
label = ttk.Label(self, width = 5)
label.grid(column = col, row = h, sticky = (N,W,E,S))
col += 1
for h in range(9):
label = ttk.Label(self, text = h+1 , width = 3)
label.grid(column = col, row = h, sticky = (N,W,E,S))
col += 1
for h in range(1,10):
button = ttk.Button(self, textvariable = self.numbersLeft[h-1], width = 3)
button.grid(column = col, row = h-1, sticky = (N,W,E,S))
def selectBtn(self, event):
global valueentry
global problem
name = event.widget["text"]
temp = valueentry.get()
try:
temp = int(temp)
if 1 <= temp <= 9:
event.widget["text"] = temp
print(event.widget["text"])
print(dir(event))
print(int(event.grid["row"]), int(event.grid["column"]))
print("hi")
problem[int(event.widget["row"])][int(event.widget["column"])] = temp
except TypeError:
pass
class SudokuApp(ttk.Frame):
def __init__(self, master):
super().__init__(master, borderwidth = 10)
self.pack()
self.create_widgets()
def create_widgets(self):
sudokuInfo = ttk.Frame(self)
sudokuInfo.pack()
timerlabel = ttk.Label(sudokuInfo, text = "Time : ")
timerlabel.pack(side = "left")
timerentry = ttk.Entry(sudokuInfo)
timerentry.pack(side = "left")
valuelabel = ttk.Label(sudokuInfo, text = "\tValue : ")
valuelabel.pack(side = "left")
global valueentry
valueentry = ttk.Entry(sudokuInfo)
valueentry.pack(side = "left")
checkBtn = ttk.Button(sudokuInfo, text = "Check Answer")
checkBtn.bind("<Button-1>", self.checkAns)
checkBtn.pack(side = "bottom")
Sudoku(self)
def checkAns(self,event):
global problem
if solved(problem):
print("Done")
else:
print("Wrong answer / incomplete")
for a in problem:
print(a)
def use_Example():
# problem = [[0, 2, 0, 5, 0, 1, 0, 9, 0],
# [8, 0, 0, 2, 0, 3, 0, 0, 6],
# [0, 3, 0, 0, 6, 0, 0, 7, 0],
# [0, 0, 1, 0, 0, 0, 6, 0, 0],
# [5, 4, 0, 0, 0, 0, 0, 1, 9],
# [0, 0, 2, 0, 0, 0, 7, 0, 0],
# [0, 9, 0, 0, 3, 0, 0, 8, 0],
# [2, 0, 0, 8, 0, 4, 0, 0, 7],
# [0, 1, 0, 9, 0, 7, 0, 6, 0]]
problem = []
file = open("Example_easy_2.txt", "r")
for line in file:
problem.append(list(map(int, line.split())))
print(problem)
return problem
def use_Solution():
problem = [[4, 2, 6, 5, 7, 1, 3, 9, 8],
[8, 5, 7, 2, 9, 3, 1, 4, 6],
[1, 3, 9, 4, 6, 8, 2, 7, 5],
[9, 7, 1, 3, 8, 5, 6, 2, 4],
[5, 4, 3, 7, 2, 6, 8, 1, 9],
[6, 8, 2, 1, 4, 9, 7, 5, 3],
[7, 9, 4, 6, 3, 2, 5, 8, 1],
[2, 6, 5, 8, 1, 4, 9, 3, 7],
[3, 1, 8, 9, 5, 7, 4, 6, 2]]
return problem
if __name__ == "__main__":
# problem = use_Solution()
problem = use_Example()
master = Tk()
master.title("Python Sudoku")
SudokuApp(master)
master.geometry("600x400")
master.mainloop()