User:ShawnDouglas/scripts/hexgui.py

From OpenWetWare

Jump to: navigation, search

Starting to play around with GUI for the honeycomb lattice nano-cad scripts --smd 08:59, 26 June 2006 (EDT)

import string
from Tkinter import *
root=Tk()
canvas=Canvas(root,width=670,height=520)
canvas.pack()

def toggle(event):
    num = string.atoi(event.widget.gettags("current")[0])
    print num
    if (fill[num-1]==0):
      canvas.itemconfig(num,fill=lightblue)
      fill[num-1]=1
    else:
      canvas.itemconfig(num,fill='white')
      fill[num-1]=0

# parameters to render hexagonal grid
x = 40
y = 30
dia = 20
offset = 10
pad = 20
n = 1
lightblue = '#ccccff'
lightyellow = '#ffffcc'
fill = []

# draw grid
for j in range(16):
  for i in range(16):
    if (j%2==0):
      canvas.create_oval(i*x+pad,j*y+pad,i*x+dia+pad,j*y+dia+pad,tags=n,fill='white')
      canvas.create_oval(i*x+dia+pad,j*y+offset+pad,i*x+dia*2+pad,j*y+dia+offset+pad,tags=n+1,fill='white')      
    else:
      canvas.create_oval(i*x+pad,j*y+offset+pad,i*x+dia+pad,j*y+dia+offset+pad,tags=n,fill='white')
      canvas.create_oval(i*x+dia+pad,j*y+pad,i*x+dia*2+pad,j*y+dia+pad,tags=n+1,fill='white')
    n += 2

# bind toggle function to click event
for i in range(1,n):
  canvas.tag_bind(i,'<Button>',func=toggle)
  fill.append(0)
  
root.mainloop()
Personal tools