Advertisement
here2share

# Tk_30x30_clickable.py

Jul 31st, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.99 KB | None | 0 0
  1. # Tk_30x30_clickable.py -- demo
  2.  
  3. from Tkinter import *
  4. import random
  5.  
  6. w,h=30,30
  7. root = Tk()
  8.  
  9. canvas = Canvas(root, width = w*20+20, height = h*20+20)
  10. canvas.pack()
  11.  
  12. cells = []
  13. for i in range (30):
  14.     '''
  15.     row = []
  16.     for j in range (30):
  17.         row.append(random.choice(['white','black']))
  18.     cells.append(row)
  19.     '''
  20.     colors=['white','black']*15 # req?
  21.     random.shuffle(colors)
  22.     cells.append(colors)
  23.  
  24. def CellMark(mode,event):
  25.     j = (event.x-10) // 20
  26.     i = (event.y-10) // 20
  27.     cells[i][j] = mode
  28.     DrawCells ()
  29.     canvas.update()  
  30.    
  31. def CellClick1(event):
  32.     CellMark('black',event)
  33.    
  34. def CellClick2(event):
  35.     CellMark('white',event)
  36.    
  37. def DrawCells():
  38.     canvas.delete('cell')
  39.     for i in range(30):
  40.         for j in range(30):
  41.             canvas.create_rectangle(10 + j*20, 10 + i*20, 10 + (j+1)*20, 10 +(i+1)*20, fill = cells[i][j], outline = "black", tag= 'cell')
  42.            
  43. canvas.tag_bind('cell', "<Button-1>", CellClick1)
  44. canvas.tag_bind('cell', "<Button-3>", CellClick2)
  45.        
  46.  
  47. DrawCells()
  48.  
  49. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement