A learning exercise...yet another Sudoku solver with GUI

E

engsolnorm

A co-worker and I want to increase our knowledge of Python. I have a tiny bit of exposure to python. He has none, but
has wide experience with C, C++, NET, C#, etc. We agreed we'd do a Sudoku solver. I'd do the GUI, he would do the
solver code.

My question is this: I assume that once I invoke mauinloop, the loop just cycles between bound events...(if not true,
please tell me)...so how do I "branch" out to the solver code, which will be in another module? Is that the function of
my "SOLVE" button..i.e., the callback associated with the SOLVE button 'asks' for an updated cell list, then writes the
cell values to the GUI? I'm stuck..:)

I've posted the entire GUI code below..please forgive the length. Usage: when a cell has the focus (mouse-over),
pressing a number key will cause that number to be displayed in that cell. Any other key will 'erase' the currently
displayed cell number, if any.

Thanks,
Norm



import tkFileDialog

fred = Tk() # Notice I have a bias against 'top', 'master', 'root', etc

fred.title(' SUDOKU SCREEN')
#fred.geometry('400x400')
#fred.resizable(0,0)

#------------------------------ Declare 9 frames plus one for the buttons

fr1 = Frame(fred); fr2 = Frame(fred); fr3 = Frame(fred)
fr4 = Frame(fred); fr5 = Frame(fred); fr6 = Frame(fred)
fr7 = Frame(fred); fr8 = Frame(fred); fr9 = Frame(fred)
bfr = Frame(fred, relief = 'raised', borderwidth = 1, pady = 10)


#------------------------------ Set some vars

ind = [3,4,5,12,13,14,21,22,23,27,28,29,33,34,35,36,37,38,42,43,44,45,46,47,51,52,53,57,58,59,66,67,68,75,76,77]
fr_list = [fr1,fr2,fr3,fr4,fr5,fr6,fr7,fr8,fr9]
can_list = ['can1','can2','can3','can4','can5','can6','can7','can8','can9']
cell = []
myvar = ''"
mykey = 'K'
myadd = 'one'
iy = 0

#------------------------------ Create 9 frames with 9 canvases in each

for fitem in fr_list:
for item in can_list:
item = Canvas(fitem, width = 30, height = 30, borderwidth = 1, relief = 'solid')
item.pack(side = LEFT, fill = BOTH)
cell.append([item, iy, '-']) # List of [IDs, 0 to 80, key text]
iy += 1


#------------------------------ Create some supporting (callback) functions

def clr_scrn():
for iz in range(81):
cell[iz][0].delete(ALL)

def hint():
print 'Hint not implemented yet'

def solve():
print 'Solve not implemented yet'

def get_file():
root = Tk()
root.withdraw() # do this to remove the "blank screen"
filepath = tkFileDialog.askopenfilename(filetypes=[("all files", "*")])
# return filepath # Do something with this later...but for now...
print filepath

def save_file():
root = Tk()
root.withdraw() # do this to remove the "blank screen"
filename = tkFileDialog.asksaveasfilename()
# return filename # Do something with this later...but for now...
print filename

def mouse_in(event):
event.widget.config(highlightcolor = 'blue')
event.widget.focus_set()

def see_key(event):
event.widget.delete(ALL)
if event.keysym in '123456789':
event.widget.create_text(18,18, text = event.keysym, font = ('arial', 18, 'bold'))
else:
event.widget.create_text(18,18, text = '', font = ('arial', 18, 'bold'))

for ia in range(81):
if event.widget is cell[ia][0]:
cell[ia][2] = event.keysym
#print cell[ia][1], ' ', cell[ia][2]

#------------------------------ Create some control buttons

b_new = Button(bfr, text = 'NEW', relief = 'raised', command = clr_scrn ).pack(side = LEFT, padx = 10)
b_open = Button(bfr, text = 'OPEN', relief = 'raised', command = get_file ).pack(side = LEFT, padx = 10)
b_save = Button(bfr, text = 'SAVE', relief = 'raised', command = save_file).pack(side = LEFT, padx = 10)
b_clr = Button(bfr, text = 'CLEAR', relief = 'raised', command = clr_scrn ).pack(side = LEFT, padx = 10)
b_hint = Button(bfr, text = 'HINT', relief = 'raised', command = hint ).pack(side = LEFT, padx = 10)
b_solve = Button(bfr, text = 'SOLVE', relief = 'raised', command = solve ).pack(side = LEFT, padx = 10)
b_exit = Button(bfr, text = 'EXIT', relief = 'raised', command = fred.quit).pack(side = LEFT, padx = 10)

#------------------------------ Now pass everything to the packer

for item in fr_list:
item.pack(side = TOP)

bfr.pack(side = TOP) # Don't forget to add the buttons

#------------------------------ Shade the sub-blocks

for ix in ind:
cell[ix][0].config(bg = 'grey')

#------------------------------ Fill in a few cells just for drill

cell[0][0].create_text(18,18, text = '3', fill = 'black', font = ('arial', 18, 'bold'))
cell[1][0].create_text(18,18, text = '6', fill = 'black', font = ('arial', 18, 'bold'))
cell[2][0].create_text(18,18, text = '9', fill = 'black', font = ('arial', 18, 'bold'))
cell[3][0].create_text(18,18, text = '7', fill = 'black', font = ('arial', 18, 'bold'))
cell[4][0].create_text(18,18, text = '5', fill = 'black', font = ('arial', 18, 'bold'))
cell[5][0].create_text(18,18, text = '8', fill = 'black', font = ('arial', 18, 'bold'))
cell[6][0].create_text(18,18, text = '4', fill = 'black', font = ('arial', 18, 'bold'))
cell[7][0].create_text(18,18, text = '2', fill = 'black', font = ('arial', 18, 'bold'))
cell[8][0].create_text(18,18, text = '1', fill = 'black', font = ('arial', 18, 'bold'))
cell[77][0].create_text(18,18, text = '5', fill = 'blue' , font = ('arial', 18, 'bold'))


for item in cell:
item[0].bind('<Enter>', mouse_in)
item[0].bind('<KeyPress>', see_key)


mainloop()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top