More sudoku
master
Voussoir 2015-01-27 23:37:44 -08:00
parent 37043fbfed
commit eef44ae127
1 changed files with 109 additions and 13 deletions

View File

@ -4,14 +4,36 @@ class Sudoku:
def __init__(self):
self.t = tkinter.Tk()
self.t.title("Sudoku")
self.t.resizable(0,0)
self.color_enterbox = "#cfc"
self.color_entertext = "#111"
self.color_background = "#222"
self.color_helptext = "#ccc"
self.checkerboard_step = 2
self.color_checkerboard = self.checkerboard(self.color_enterbox)
self.font_enterbox = ["Consolas", 16]
self.font_helptext = ["Consolas", 10]
self.relief_enterbox = "ridge"
# flat, groove, raised, ridge, solid, sunken
self.docheckerboard = True
self.allow_wraparound = True
self.entry_square = 50
self.spacer_width = 3
self.cursor_position = [0,0]
self.misc_height = 0
self.window_square = (9 * self.entry_square) + (2 * self.spacer_width)
self.helptext = "W,A,S,D to easily move around; E to clear cell; Enter to grade"
self.create_helptext(self.helptext)
self.t.configure(width=9*self.entry_square, height=9*self.entry_square)
self.t.configure(bg="#000")
self.t.configure(width=self.window_square, height=self.window_square+self.misc_height)
self.t.configure(bg=self.color_background)
self.entities_entry = []
self.permanents = []
self.create_boxes()
self.t.bind("<KeyPress>", self.keypress)
@ -23,8 +45,7 @@ class Sudoku:
}
self.key_clearcurrent = ["e"]
self.permanents[]
self.cursor_position = [0,0]
self.select_entry_by_pos(self.cursor_position)
self.t.mainloop()
@ -40,6 +61,21 @@ class Sudoku:
index = (9 * y) + x
self.entities_entry[index].delete(0, "end")
def create_helptext(self, helptext):
helplabel = tkinter.Label(self.t, text=helptext)
helplabel.configure(font=self.font_helptext,
fg=self.color_helptext,
bg=self.color_background,
justify="left")
height = (2.3 * self.font_helptext[1])
height *= len(helptext.split('\n'))
height += self.spacer_width
print('Helptext added %d pixels in height' % height)
self.misc_height += height
helplabel.place(x=0, y=self.window_square + self.spacer_width)
def create_boxes(self):
spacer_y = 0
for y in range(9):
@ -53,27 +89,73 @@ class Sudoku:
spacer_x += self.spacer_width
xpos = (x * self.entry_square) + spacer_x
stringvar = tkinter.StringVar()
enter = tkinter.Entry(self.t)
stringvar = tkinter.StringVar()
enter.stringvar = stringvar
enter.stringvar.trace("w", lambda name,index,mode, stringvar=stringvar: self.checkinput(stringvar))
enter.configure(justify="c", textvariable=enter.stringvar)
enter.configure(font= ("Consolas", 14))
enter.name = "%d, %s" % (x,y)
bg = self.color_enterbox
relief = self.relief_enterbox
if self.docheckerboard:
docheckerboard = (str(x+y)[-1] in "13579")
if docheckerboard:
bg = self.color_checkerboard
enter.configure(justify="c",
textvariable=enter.stringvar,
font=self.font_enterbox,
bg=bg,
fg=self.color_entertext,
relief=relief)
enter.bind("<Button-1>", self.update_position_byclick)
enter.coordinates = [x, y]
self.entities_entry.append(enter)
enter.place(x=xpos, y=ypos, width=self.entry_square, height=self.entry_square)
def checkerboard(self, hexivalue):
hexivalue = hexivalue[1:]
if len(hexivalue) == 3:
r = hexivalue[0]
g = hexivalue[1]
b = hexivalue[2]
padding = "1"
if len(hexivalue) == 6:
r = hexivalue[:2]
g = hexivalue[2:4]
b = hexivalue[4:]
padding = "2"
hexiout = "#"
for colorcomponent in [r,g,b]:
decivalue = int(colorcomponent, 16)
if decivalue < self.checkerboard_step:
decivalue += self.checkerboard_step
else:
decivalue -= self.checkerboard_step
formatstring = "%0" + padding + "x"
hexivalue = formatstring % decivalue
hexiout += hexivalue
return hexiout
def checkinput(self, *bullish):
stringvar = bullish[0]
stringvalue = stringvar.get()
try:
int(stringvalue)
except ValueError:
test_for_integer= int(stringvalue)
test_for_nonzero= 14/test_for_integer
except:
stringvar.set("")
if len(stringvalue) > 1:
stringvar.set(stringvalue[0])
def update_position_byclick(self, event):
enter = event.widget
x = enter.coordinates[0]
y = enter.coordinates[1]
self.cursor_position = [x,y]
def select_entry_by_pos(self, position):
x = position[0]
y = position[1]
@ -86,10 +168,24 @@ class Sudoku:
xposition = self.cursor_position[0]
yposition = self.cursor_position[1]
if (xposition > 0 or xdirection >= 0) and (xposition < 8 or xdirection <= 0):
hasmoved = False
if (xposition > 0 or xdirection == 1) and (xposition < 8 or xdirection == -1) and (xdirection != 0):
xposition += xdirection
if (yposition > 0 or ydirection >= 0) and (yposition < 8 or ydirection <= 0):
hasmoved = True
if (yposition > 0 or ydirection == 1) and (yposition < 8 or ydirection == -1) and (ydirection != 0):
yposition += ydirection
hasmoved = True
if self.allow_wraparound and hasmoved is False:
if xposition == 0 and xdirection == -1:
xposition = 8
elif xposition == 8 and xdirection == 1:
xposition = 0
elif yposition == 0 and ydirection == -1:
yposition = 8
elif yposition == 8 and ydirection == 1:
yposition = 0
self.cursor_position = [xposition, yposition]
self.select_entry_by_pos(self.cursor_position)