473,756 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


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,fr 8,fr9]
can_list = ['can1','can2',' can3','can4','c an5','can6','ca n7','can8','can 9']
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.as kopenfilename(f iletypes=[("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.as ksaveasfilename ()
# return filename # Do something with this later...but for now...
print filename

def mouse_in(event) :
event.widget.co nfig(highlightc olor = 'blue')
event.widget.fo cus_set()

def see_key(event):
event.widget.de lete(ALL)
if event.keysym in '123456789':
event.widget.cr eate_text(18,18 , text = event.keysym, font = ('arial', 18, 'bold'))
else:
event.widget.cr eate_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('<KeyPres s>', see_key)
mainloop()

Jan 19 '06 #1
0 1774

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
4139
by: ago | last post by:
Inspired by some recent readings on LinuxJournal and an ASPN recipe, I decided to revamp my old python hack... The new code is a combination of (2) reduction methods and brute force and it is quite faster than the ASPN program. If anyone is interested I attached the code in http://agolb.blogspot.com/2006/01/sudoku-solver-in-python.html
12
6345
by: kalinga1234 | last post by:
hy guys i am having a problem with my sudoku program which i coded using c++.; currently in my program if a duplicate number exist in either row/column/block i would make the particualr square 0. but thats not i want to do. I want to recurse back until until it find a correct number. i will post the function which i need the help; ---coding----------------------------------------------------------
2
2205
by: avinash1990 | last post by:
hi can you tell me how to make a sudoku solver program ..i really need it for my project
0
6740
by: JosAH | last post by:
Greetings, a couple of years ago a large part of the world went totally mad. Not because of global climate changes, not because of terrible wars that were started in the Middle East, nor because of global famine, but because of a puzzle: Sudoku. This is what Sudoku is all about: +-------+-------+-------+
6
11882
by: blux | last post by:
I am working on a function to check the validity of a sudoku puzzle. It must check the 9x9 matrix to make sure it follows the rules and is a valid sudoku puzzle. this is what I have come up with so far: However I have found that it does not check it correctly. I just need to check the 9x9 array, which I am passing to this function against the classic sudoku rules and then return true for
1
2040
Thekid
by: Thekid | last post by:
Hi, I found this sudoku solver online and it works good but I was wondering how I could get the answer that's in matrix form, to also print out in a single comma-delimited line, instead of 9 rows of 9? from copy import deepcopy class DeadEnd(Exception): pass class Matrix: def __init__(self, input): self.rows = for i in range(9)]
38
6391
by: Boon | last post by:
Hello group, I've been toying with a simple sudoku solver. I meant for the code to be short and easy to understand. I figured "brute force is simple" -- who needs finesse, when you've got muscle, right? :-) http://en.wikipedia.org/wiki/Sudoku Thus, the strategy is to test every legal "move" and to backtrack when stuck. A recursive function seemed appropriate. I'd like to hear
62
12252
jkmyoung
by: jkmyoung | last post by:
Does anyone have some super, super hard Sudoku puzzles? Back in February this year, I had enough time to finally program a Sudoku solver in Java. Right now, I'm looking for solvable puzzles, but ones that my program cannot solve. However, most hard puzzles in the newspapers are solved within 2-3 cycles so far. The ones I've found on google which are said to be hard, get solved in 3 cycles. Any help would be very much appreciated!...
3
3400
by: Ri0o | last post by:
hi, i have to make a sudoku solver using python quickdraw, i've started on it and this is what i got so far here is the link to the assignment http://pages.cpsc.ucalgary.ca/~zongpeng/CPSC231/assignments/A4/a4.pdf def solveRows(): """eliminates solved numbers by row""" for x in range(0, 81, 9): row = puzzle#loops through each row for space in row: #loops through each space if len(space) == 1: #space is...
0
9431
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9819
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8688
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7226
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6514
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5119
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5289
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3326
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2647
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.