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
| from Tkinter import *
from random import shuffle
# ------------------------------------------------------------------------------
def cb_button(flag):
"""fonction de rappel pour le "SHUFFLE" et le bouton 'RESET' """
global hole,size
cols, hole = (int(dim *(dim+1)/2)) * range(4), (dim, dim)
if flag: shuffle(cols) # mélanger les couleurs si ' Shuffle' a été pressé
for n in range(int(size*size)):
cell, cols = divmod(n, size), cols[n % len(cols)]
root[cell]['relief'], root[cell]['bg'] = RAISED, colors[col]
root[hole]['relief'], root[hole]['bg'] = SUNKEN, colors[4]
# ------------------------------------------------------------------------------
def cb_cell(cell):
"""fonction de rappel pour chaque cellule de la carte"""
global hole
p, q = cell[0] - hole[0], cell[1] - hole[1]
if not (p or q) or (p and q): return # ne peut pas glisser cellule courante
if p: #glissière horizontale
dp, q = p / abs(p), hole[1]
for p in range(hole[0], cell[0], dp):
root[p,q]['relief'], root[p,q]['bg'] = RAISED, root[p+dp,q]['bg']
else: # glissière verticale
dq, p = q / abs(q), hole[0]
for q in range(hole[1], cell[1], dq):
root[p,q]['relief'], root[p,q]['bg'] = RAISED, root[p,q+dq]['bg']
hole = cell; root[hole]['relief'], root[hole]['bg'] = SUNKEN, colors[4]
# ------------------------------------------------------------------------------
def main():
"""programme principal du module 'slide' """
global root, dim, size, hole, colors
root, dim, fnt = {0: Tk()}, 2, 'Arial 12'
size, colors = 2*dim+1, ('#f00','#0f0','#00f','#ff0','#fff')
# ----------------------------------------------------------------------------
root[1] = Frame(root[0])
root[1].pack(side=TOP, fill=BOTH, padx=5, pady=5)
root[11] = Button(root[1], font=fnt, width=8, text='SHUFFLE',
command=lambda: cb_button(True))
root[11].pack(side=LEFT, fill=BOTH, expand=YES)
root[12] = Button(root[1], font=fnt, width=8, text='RESET',
command=lambda: cb_button(False))
root[12].pack(side=LEFT, fill=BOTH, expand=YES)
# ----------------------------------------------------------------------------
root[2] = Frame(root[0])
root[2].pack(side=TOP, fill=BOTH, expand=YES, padx=5, pady=5)
for n in range(size*size):
c = divmod(n, size)
root[c] = Label(root[2], font=fnt, width=6, height=3, relief=RAISED)
root[c].bind('<Button-1>', lambda event, cell=c: cb_cell(cell))
root[c].grid(column=c[0], row=c[1], sticky=N+E+W+S)
for n in range(size): root[2].columnconfigure(n, weight=1)
for n in range(size): root[2].rowconfigure(n, weight=1)
root[12].invoke() # configuration grille initiale en invoquant 'reset' Bouton
# ----------------------------------------------------------------------------
root[0].title('SLIDE'); root[0].protocol('WM_DELETE_WINDOW', root[0].quit)
root[0].update_idletasks(); root[0].resizable(1,1);
root[0].minsize(root[0].winfo_width(), root[0].winfo_height())
root[0].mainloop(); root[0].destroy()
# ------------------------------------------------------------------------------
if __name__ == '__main__':
main() |
Partager