def draw_dna(): logging.debug('loading cell pallate') pallate = cellPallate() pallate.load() logging.debug('getting default pallate selection') chosenCell = pallate.getUserChoice() chosenName = pallate.getSelectedCellName() g.show('now drawing with DNA from '+chosenName) logging.debug('now drawing with DNA from '+chosenName+'='+str(chosenCell)) # prepare environment env = environment() logging.debug('turning on golly even script access') event = g.getevent(True) # turn on golly event script access # === let user draw g.setcursor("Draw") try: #this try statement is just to ensure the 'finally' block is run while True: # loop until stop button is pressed event = g.getevent() # event is a string like "click 10 20 left none" if len(event) < 1: # do not try to split empty string continue else: logging.debug('event recieved: "'+event+'"') evt, xstr, ystr, butt, mods = event.split() if evt=="click" and butt=="left" and mods=="none": # left click # logging.debug('left click detected at '+xstr+','+ystr) x = int(xstr) y = int(ystr) env.cellList.setCell(x,y,cell=cell(x,y,chosenCell.DNA)) #add cell to list g.setcell(x,y,1) #fill in functional cell env.drawColor() #update color layer to match g.update() #update golly display logging.info('cell ('+xstr+','+ystr+') painted with "'+chosenName+'"') g.show('cell painted. press "Esc" to stop drawing') else: logging.info('event "'+event+'" not recognized') except: # re-raise any errors encountered logging.error('unexpected error: '+sys.exc_info()[0]) raise finally: g.getevent(False) # return event handling to golly g.show('done drawing '+chosenName+' cells.') logging.debug('done drawing '+chosenName+' cells.') # === teardown env.teardown() return
def collect_dna(): # prepare environment env = environment() # ask user to select cell of interest g.setcursor("Pick") g.show('select cell to sample') event = g.getevent(True) #turn on golly event script access while not event.startswith("click"): event = g.getevent() # return event handling to golly # event is a string like "click 10 20 left none" g.getevent(False) # return event handling to golly evt, xstr, ystr, butt, mods = event.split() x = int(xstr) y = int(ystr) logging.info('cell ('+xstr+','+ystr+') selected') try: # retrieve selected cell selectedCell = env.cellList.findCell(x,y) except AttributeError: g.show('cannot find cell!') logging.error('cell not found. len(cellList)='+str(len(cellList.cells))) env.teardown() return # prompt user for name import Tkinter as tk root = tk.Tk() class selectorDisplay: def __init__(self, master, selectedCell): self.pallate = cellPallate() # cell pallate instance for saving cell info self.frame = tk.Frame(master) self.frame.pack() self.cell = selectedCell instructions = tk.Label(root, text='Please enter a name for this cell.\n\ NOTE: names should only consist of letters, numbers, "_", and "-"') instructions.pack() self.entry = tk.Entry(master) self.entry.pack() self.entry.focus_set() button_save = tk.Button(master, text="save", width=10, command=self.submitEntry) button_save.pack() button_cancel = tk.Button(master, text="cancel", width=10, command=self.frame.quit) button_cancel.pack() def submitEntry(self): # save the cell name = self.entry.get() g.show('saving ' + name + ' to ' + CELL_COLLECTION_DIR) self.pallate.saveCell(self.cell,name) self.frame.quit() # close dialog g.show('DNA sample saved to collection') app = selectorDisplay(root,selectedCell) root.mainloop() import _tkinter try: root.destroy() # optional...ish except _tkinter.TclError: pass # ignore failed destroy due to already being destroyed. env.teardown() return