Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_30x30_clickable.py -- demo
- from Tkinter import *
- import random
- w,h=30,30
- root = Tk()
- canvas = Canvas(root, width = w*20+20, height = h*20+20)
- canvas.pack()
- cells = []
- for i in range (30):
- '''
- row = []
- for j in range (30):
- row.append(random.choice(['white','black']))
- cells.append(row)
- '''
- colors=['white','black']*15 # req?
- random.shuffle(colors)
- cells.append(colors)
- def CellMark(mode,event):
- j = (event.x-10) // 20
- i = (event.y-10) // 20
- cells[i][j] = mode
- DrawCells ()
- canvas.update()
- def CellClick1(event):
- CellMark('black',event)
- def CellClick2(event):
- CellMark('white',event)
- def DrawCells():
- canvas.delete('cell')
- for i in range(30):
- for j in range(30):
- 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')
- canvas.tag_bind('cell', "<Button-1>", CellClick1)
- canvas.tag_bind('cell', "<Button-3>", CellClick2)
- DrawCells()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement