Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_Sudoku.py -- Generic GUI
- from Tkinter import *
- from PIL import ImageGrab,ImageTk
- import random,time,datetime
- root=Tk()
- root.title("Tk Pydoku")
- sq=60
- numsq=9
- w=4
- prev=0,0
- xm=ym=0
- y=9*sq+6
- midsq=int(sq/2)+(w/2)
- level=(36,30,27,24,21,18) # (0 to 5) from easy to expert
- hints=level[0]
- aaa='abcdefghxdgfxhabcehxecgbfdafedgxhabcxhgabcefdbcaefdxgheahbdgcxfgdxfcehabcfbhaxdeg'
- bbb='..X.X.X..X.X......X.........X..X..X........XX........X.X....X..X..X........X.....'
- grid=range(81)
- numbers=range(1,10)
- cue=[0,3,6,27,30,33,54,57,60]
- ccc=[0,1,2,9,10,11,18,19,20]
- box=[[i+ii for i in ccc] for ii in cue]
- clock_bg=sq*3+6,10*sq+6,6*sq,11*sq
- clockwork=sq*4+midsq,sq*10+20
- rgb_102='grey40' ### '#'+('%02x' % (102))*3
- root.geometry("{0}x{1}+10+10".format(9*sq+8,11*sq+8))
- cvw,cvh=numsq*sq+4,11*sq+8
- canvas=Canvas(root,width=cvw,height=cvh)
- def grabwidget(widget,xy):
- xx,yy,x2,y2=xy
- x=widget.winfo_rootx()
- y=widget.winfo_rooty()
- w=widget.winfo_width()
- h=widget.winfo_height()
- time.sleep(0.3)
- img=ImageGrab.grab((x+xx,y+yy,x+x2,y+y2))
- return ImageTk.PhotoImage(img)
- xy9=6,6+sq*9,sq*9,sq*10
- canvas.create_rectangle(0,0,sq*10,sq*12,fill='grey',width=0)
- for i in range(1,10):
- canvas.create_text(sq*i-midsq+w,sq*9+midsq,font=("Calibri",sq-8),text=i,fill='grey40')
- canvas.pack()
- canvas.update()
- img9=grabwidget(canvas,xy9)
- def curSq():
- canvas.create_rectangle(xm+2+w,ym+2+w,xm-2+sq+w,ym-2+sq+w,fill='green',width=0)
- #
- def alive():
- canvas.create_text(sq*active-midsq+w,sq*9+midsq,font=("Calibri",sq-8),text=active,fill='yellow')
- def line9():
- canvas.create_image(xy9[:2],anchor=NW,image=img9)
- curSq()
- canvas.create_text(xm+midsq,sq*9+midsq,font=("Calibri",sq-8),text=xm/sq+1,fill='grey40')
- alive()
- #
- def update_check(xx,yy,z):
- if yy/sq < 9:
- xx=xx+midsq
- yy=yy+midsq
- tmp=(xx/sq)*9+(yy/sq)
- c='black'
- if mask[tmp] is ' ':
- c='blue'
- if mode is '9x9' and z is 1:
- if check[tmp] is ' ': check[tmp]=str(active)
- else: check[tmp]=' '
- canvas.create_text(xx,yy,font=("Calibri",sq-8),text=check[tmp],fill=c)
- #
- def callback(event):
- global mode
- x,y=event.x,event.y
- ### print "clicked at",x,y
- if check != solution:
- if y < sq*9+w: mode='9x9'; return
- else: mode=x/sq+1
- if y > sq*10+w:
- if x > sq*6+w: mode='quit'
- elif x < sq*3+w and s >= '01': mode='new'
- root.bind("<Button-1>",callback)
- def key(event):
- global mode
- if check != solution:
- e=str(event.char)
- if e.isdigit() and e != '0':
- mode=int(e)
- root.bind("<Key>",key)
- def motion(event):
- global xm,ym
- x,y=event.x/sq,event.y/sq
- if x > 8 or y > 9: return
- xm=x*sq; ym=y*sq
- root.bind('<Motion>',motion)
- def progress(z):
- for i in range(9):
- print
- for ii in range(9):
- print z[ii*9+i],
- print
- print
- def shfl(z):
- z=[[z[i*9+ii] for i in range(9)] for ii in range(9)]
- z=[z[0:3],z[3:6],z[6:9]]
- for i in range(3):
- random.shuffle(z[i])
- random.shuffle(z)
- return sum(sum(z,[]),[])
- def gen():
- sq=grid
- random.shuffle(numbers) # for an added measure
- for i in range(numbers[-1]):
- sq=shfl(sq)
- a=b=''
- for i in sq:
- a+=aaa[i]
- b+=bbb[i]
- for i in range(9):
- tmp=str(numbers[i])
- a=a.replace(a[i],tmp)
- b=b.replace(a[i],tmp)
- random.shuffle(sq)
- b=list(b)
- for i in sq:
- if b[i] is 'X': b[i]=a[i]
- elif 81-b.count('.') < hints:
- for z in box:
- if i in z:
- c=''.join([b[r] for r in z])
- break
- c=c.count('.')
- if c > 5:
- b[i]=a[i]
- b=''.join(b)
- progress(a)
- progress(b)
- print 81-b.count('.'),'hints'
- print
- print b
- b=b.replace('.',' ')
- return list(a),b
- #
- init=True
- while init:
- solution,mask=gen()
- check=list(mask[:])
- canvas.create_rectangle(0,0,11*sq+w,11*sq+w,fill='white')
- for yy in range(9):
- for xx in range(9):
- x2=xx*sq+midsq
- y2=yy*sq+midsq
- canvas.create_text(x2,y2,font=("Calibri",sq-8),text=mask[xx*9+yy])
- a=(sq,sq*numsq,sq),'grey'
- b=(0,sq*(numsq+1),sq*3),'black'
- canvas.create_rectangle(w,9*sq+w,9*sq+w,11*sq+w,fill='grey')
- canvas.create_rectangle(0,sq*10+w,sq*3+w,sq*11+w,fill='green',width=0)
- canvas.create_rectangle(sq*6+w,sq*10+w,sq*9+w,sq*11+w,fill='red',width=0)
- for r,c in [a,b]:
- for i in range(*r): ### will unpack lists and tuples
- x=i+w
- # draw horizontal lines
- canvas.create_line(0,x,y,x,width=w,fill=c)
- # draw vertical lines
- canvas.create_line(x,0,x,y,width=w,fill=c)
- for i in [sq*3,sq*6]:
- x=i+w
- canvas.create_line(x,sq*10+w,x,sq*11+w,width=w,fill=c)
- canvas.create_rectangle(w,9*sq+w,9*sq+w,11*sq+w,width=w)
- canvas.create_line(0,sq*10+w,sq*9+w,sq*10+w,width=w)
- active=1
- mode='initiated'
- line9()
- dt=datetime.datetime.now()
- while mode:
- if check is solution: pass
- elif prev != (xm,ym) or mode != 'standby':
- x2,y2=prev
- prev=xm,ym
- if mode in numbers: active=mode
- c='white'
- if y2/sq is 9:
- c='grey'
- canvas.create_rectangle(x2+2+w,y2+2+w,x2-2+sq+w,y2-2+sq+w,fill=c,width=0)
- if sq*9 in (ym,y2) or mode in numbers:
- line9()
- elif check: curSq()
- update_check(xm,ym,1)
- update_check(x2,y2,0)
- mode='standby'
- ss=str(datetime.datetime.now()-dt)
- h,m,s=ss.split('.')[0].split(':')
- try:
- if check != solution:
- clock='%s:%s' % (m,s)
- if m > '20': clock='+20 Mins?'
- canvas.create_rectangle(*clock_bg,fill='grey',width=0)
- canvas.create_text(*clockwork,font=("Ariel",16),text=clock)
- elif check != '':
- canvas.create_rectangle(0,0,sq*9+w,sq*9+w,fill='black',width=0)
- for yy in range(9):
- for xx in range(9):
- x2=xx*sq+6
- y2=yy*sq+6
- c,t='light blue','black'
- area=mask[xx*9+yy]
- if area is ' ':
- area=solution[xx*9+yy]
- t='white'
- canvas.create_rectangle(x2,y2,x2+(sq-w),y2+(sq-w),fill=c,width=0)
- canvas.create_text(x2+midsq-w,y2+midsq-6,font=("Calibri",sq-8),text=area,fill=t)
- canvas.create_image(xy9[:2],anchor=NW,image=img9)
- solution=check=''
- print "Board Completed @",clock # ZZZ
- canvas.update()
- if mode is 'quit': root.destroy(); Trigger-Exception
- elif mode is'new':
- canvas.create_rectangle(0,7*sq+w,9*sq+6,9*sq+w,fill='purple',width=0)
- canvas.create_text(sq*4+midsq,sq*8,font=("Ariel",10*sq/20),text="STARTING NEW GAME...",fill='white')
- canvas.update()
- mode=False
- except: mode=init='' ;root.quit()
- root.mainloop()
Add Comment
Please, Sign In to add comment