Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_array_click.py
- try:
- # Python2
- from Tkinter import *
- except ImportError:
- # Python3
- from tkinter import *
- #
- r,c=8,8
- grid=[[-1]*r for n in range(c)] # list comprehension
- grid[0][0]=1
- grid[7][7]=1
- w=70 # width of each cell
- root=Tk()
- root.geometry('{}x{}+0+0'.format(w*r,w*c)) #.format(root.winfo_screenwidth())
- root.title('2D')
- canvas=Canvas(root, width=1200, height=1200, background='grey')
- def draw_grid():
- x,y=0,0 # starting position
- for row in grid:
- for col in row:
- color='white'
- if col == 1:
- color='red'
- canvas.create_rectangle(x, y, x+w, y+w, fill=color)
- x+=w
- y+=w
- x=0
- #
- def mouse(event):
- x,y=event.x/w,event.y/w
- print
- print "clicked at", event.x, event.y, '\t\t',(x,y)
- grid[y][x]=-1 * grid[y][x] # Note: grid[Y][X] not grid[X][Y]
- color='white'
- if grid[y][x] == 1: color='red'
- x*=w
- y*=w
- canvas.create_rectangle(x, y, x+w, y+w, fill=color)
- #
- canvas.bind("<Button-1>", mouse)
- draw_grid()
- canvas.pack()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement