def getfile(self): fname = tkFileDialog.askopenfilename() #print "doing stuff with "+fname try: tree = ET.parse(fname) treeroot = tree.getroot() self.Log("\nLoad " + fname + "\n") gluefun = treeroot[0] #flush board del self.board.Polyominoes[:] self.board.LookUp = {} TT.GLUEFUNC = {} self.board = TT.Board(TT.BOARDHEIGHT, TT.BOARDWIDTH) for fun in gluefun: TT.GLUEFUNC[fun.find('Labels').attrib['L1']] = int( fun.find('Strength').text) for tt in treeroot[1:]: ntlocx = 0 ntlocy = 0 if tt[0].find('Location') != None: ntlocx = int(tt[0].find('Location').attrib['x']) ntlocy = int(tt[0].find('Location').attrib['y']) ntcol = "#555555" if tt[0].find('Color') != None: ntcol = "#" + tt[0].find('Color').text ntnort = " " if tt[0].find('NorthGlue') != None: ntnort = tt[0].find('NorthGlue').text nteast = " " if tt[0].find('EastGlue') != None: nteast = tt[0].find('EastGlue').text ntsouth = " " if tt[0].find('SouthGlue') != None: ntsouth = tt[0].find('SouthGlue').text ntwest = " " if tt[0].find('WestGlue') != None: ntwest = tt[0].find('WestGlue').text ntlab = "X" if tt[0].find('label') != None: ntlab = tt[0].find('label').text ntile = TT.Tile(ntlab, ntlocx, ntlocy, [ntnort, nteast, ntsouth, ntwest], ntcol) self.board.Add(TT.Polyomino(ntile)) self.board.SetGrid() self.RedrawCanvas() #board.GridDraw() #print TT.GLUEFUNC except: print "Problem with file: " + fname print sys.exc_info()[0]
def CreateInitial(self): self.Log("\nLoad initial\n") #flush board del self.board.Polyominoes[:] self.board.LookUp = {} self.board = TT.Board(TT.BOARDHEIGHT, TT.BOARDWIDTH) bh = TT.BOARDHEIGHT bw = TT.BOARDWIDTH TT.GLUEFUNC = { 'N': 1, 'E': 1, 'S': 1, 'W': 1, } #initial #CreateTiles(board) colorb = "#000" colorl = "#fff" NumTiles = 10 for i in range(NumTiles): #bottom tiles #colorb = str(colorb[0]+chr(ord(colorb[1])+1)+colorb[2:]) colorb = "#" + str(hex(random.randint(0, 16))[2:]) + str( hex(random.randint(0, 16))[2:]) + str( hex(random.randint(0, 16))[2:]) if len(colorb) > 4: colorb = colorb[:4] p = TT.Polyomino( TT.Tile(chr(ord('A') + i), 0, bh - i - 2, ['N', 'E', 'S', 'W'], colorb)) self.board.Add(p) #left tiles #colorl = str(colorl[0]+chr(ord(colorl[1])-1)+colorl[2:]) colorl = "#" + str(hex(random.randint(0, 16))[2:]) + str( hex(random.randint(0, 16))[2:]) + str( hex(random.randint(0, 16))[2:]) if len(colorl) > 4: colorl = colorl[:4] p = TT.Polyomino( TT.Tile(chr(ord('a') + i), i + 1, bh - 1, ['S', 'W', 'N', 'E'], colorl)) self.board.Add(p) self.board.SetGrid() self.RedrawCanvas()
def parseFile(filename): tree = ET.parse(filename) treeroot = tree.getroot() #self.Log("\nLoad "+filename+"\n") #default size of board, changes if new board size data is read from the file rows = 15 columns = 15 boardSizeExits = False glueFuncExists = False previewTilesExist = False tileDataExists = False CommandsExists = False #check if the xml attributes are found if tree.find("GlueFunction") != None: glueFuncExists = True if tree.find("PreviewTiles") != None: previewTilesExist = True if tree.find("BoardSize") != None: boardSizeExists = True if tree.find("TileData") != None: tileDataExists = True if tree.find("Commands") != None: CommandsExists = True #data set that will be passed back to tumblegui tile_set_data = {"glueFunc": {}, "prevTiles": [], "tileData": []} if boardSizeExists: rows = treeroot[0].attrib["height"] columns = treeroot[0].attrib["width"] #add glue function to the data set if glueFuncExists: glueFuncTree = treeroot[1] for fun in glueFuncTree: tile_set_data["glueFunc"][fun.find('Labels').attrib['L1']] = ( fun.find('Strength').text) #add preview tiles to the data set if previewTilesExist: prevTilesTree = treeroot[2] for prev in prevTilesTree: newPrevTile = {} newPrevTile["color"] = "#555555" newPrevTile["northGlue"] = " " newPrevTile["southGlue"] = " " newPrevTile["westGlue"] = " " newPrevTile["eastGlue"] = " " newPrevTile["label"] = "X" newPrevTile["concrete"] = " " if prev.find('Color') != None: if prev.find('Concrete').text == "True": newPrevTile["color"] = "#686868" else: newPrevTile["color"] = "#" + prev.find('Color').text if prev.find('NorthGlue') != None: newPrevTile["northGlue"] = prev.find('NorthGlue').text if prev.find('EastGlue') != None: newPrevTile["eastGlue"] = prev.find('EastGlue').text if prev.find('SouthGlue') != None: newPrevTile["southGlue"] = prev.find('SouthGlue').text if prev.find('WestGlue') != None: newPrevTile["westGlue"] = prev.find('WestGlue').text if prev.find('label') != None: newPrevTile["label"] = prev.find('label').text if prev.find('Concrete') != None: newPrevTile["concrete"] = prev.find('Concrete').text tile_set_data["prevTiles"].append(newPrevTile) #add tile data to the data set, these are the tiles that will actually be loaded onto the plane if tileDataExists: tileDataTree = treeroot[3] for tile in tileDataTree: newTile = {} newTile["location"] = {'x': 0, 'y': 0} newTile["color"] = "#555555" newTile["northGlue"] = " " newTile["southGlue"] = " " newTile["westGlue"] = " " newTile["eastGlue"] = " " newTile["label"] = "X" newTile["concrete"] = " " if tile.find('Location') != None: newTile["location"]["x"] = int( tile.find('Location').attrib['x']) newTile["location"]["y"] = int( tile.find('Location').attrib['y']) if tile.find('Color') != None: if tile.find('Concrete').text == "True": newTile["color"] = "#686868" else: newTile["color"] = "#" + tile.find('Color').text if tile.find('NorthGlue') != None: newTile["northGlue"] = tile.find('NorthGlue').text if tile.find('EastGlue') != None: newTile["eastGlue"] = tile.find('EastGlue').text if tile.find('SouthGlue') != None: newTile["southGlue"] = tile.find('SouthGlue').text if tile.find('WestGlue') != None: newTile["westGlue"] = tile.find('WestGlue').text if tile.find('label') != None: newTile["label"] = tile.find('label').text if tile.find('Concrete') != None: newTile["concrete"] = tile.find('Concrete').text tile_set_data["tileData"].append(newTile) board = TT.Board(int(rows), int(columns)) glueFunc = tile_set_data["glueFunc"] prevTiles = tile_set_data["prevTiles"] prevTileList = [] for tile in tile_set_data["tileData"]: if tile["concrete"] != "True": glues = [ tile["northGlue"], tile["eastGlue"], tile["southGlue"], tile["westGlue"] ] board.Add( TT.Polyomino(0, tile["location"]["x"], tile["location"]["y"], glues, tile["color"])) else: glues = [] board.AddConc( TT.Tile(None, 0, tile["location"]["x"], tile["location"]["y"], glues, tile["color"], "True")) for prevTile in prevTiles: prevGlues = [ prevTile["northGlue"], prevTile["eastGlue"], prevTile["southGlue"], prevTile["westGlue"] ] prevTileList.append( TT.Tile(None, 0, 0, 0, prevGlues, prevTile["color"], prevTile["concrete"])) commands = [] if CommandsExists: listOfCommands = treeroot[4] print(listOfCommands) for c in listOfCommands: print(c) print("NAME: ", c.attrib["name"], " FILENAME: ", c.attrib["filename"]) commands.append((c.attrib["name"], c.attrib["filename"])) data = [board, glueFunc, prevTileList, commands] return data
def __init__(self, root): global TILESIZE self.board = TT.Board(TT.BOARDHEIGHT, TT.BOARDWIDTH) self.root = root self.root.resizable(False, False) self.mainframe = Frame(self.root, bd=0, relief=FLAT) #main canvas to draw on self.w = Canvas(self.mainframe, width=TT.BOARDWIDTH * TILESIZE, height=TT.BOARDHEIGHT * TILESIZE) #mouse self.w.bind("<Button-1>", self.callback) #arrow keys self.root.bind("<Up>", self.key) self.root.bind("<Right>", self.key) self.root.bind("<Down>", self.key) self.root.bind("<Left>", self.key) self.w.pack() #menu #menu - https://www.tutorialspoint.com/python/tk_menu.htm self.menubar = Menu(self.root, relief=FLAT) filemenu = Menu(self.menubar, tearoff=0) filemenu.add_command(label="Example", command=self.CreateInitial) filemenu.add_command(label="Load", command=self.getfile) self.tkLOG = BooleanVar() self.tkLOG.set(False) filemenu.add_checkbutton(label="Log Actions", onvalue=True, offvalue=False, variable=self.tkLOG, command=self.EnableLogging) if PYSCREEN == True: filemenu.add_command(label="Picture", command=self.picture) else: filemenu.add_command(label="Picture", command=self.picture, state=DISABLED) filemenu.add_separator() filemenu.add_command(label="Exit", command=self.root.quit) aboutmenu = Menu(self.menubar, tearoff=0) aboutmenu.add_command(label="About", command=self.about) self.tkSTEPVAR = BooleanVar() self.tkSTEPVAR.set(False) self.tkGLUESTEP = BooleanVar() self.tkGLUESTEP.set(False) self.tkDRAWGRID = BooleanVar() self.tkDRAWGRID.set(False) self.tkSHOWLOC = BooleanVar() self.tkSHOWLOC.set(False) settingsmenu = Menu(self.menubar, tearoff=0) settingsmenu.add_checkbutton( label="Single Step", onvalue=True, offvalue=False, variable=self.tkSTEPVAR) #,command=stepmodel) settingsmenu.add_checkbutton( label="Glue on Step", onvalue=True, offvalue=False, variable=self.tkGLUESTEP) #,state=DISABLED) settingsmenu.add_separator() settingsmenu.add_command(label="Background Color", command=self.changecanvas) settingsmenu.add_checkbutton(label="Show Grid", onvalue=True, offvalue=False, variable=self.tkDRAWGRID, command=self.RedrawCanvas) settingsmenu.add_command(label="Grid Color", command=self.changegridcolor) settingsmenu.add_checkbutton(label="Show Locations", onvalue=True, offvalue=False, variable=self.tkSHOWLOC, command=self.RedrawCanvas) settingsmenu.add_separator() settingsmenu.add_command(label="Board Options", command=self.changetile) self.menubar.add_cascade(label="File", menu=filemenu) self.menubar.add_cascade(label="Settings", menu=settingsmenu) self.menubar.add_cascade(label="Help", menu=aboutmenu) self.root.config(menu=self.menubar) #toolbar #http://zetcode.com/gui/tkinter/menustoolbars/ self.toolbar = Frame(self.mainframe, bd=0, relief=FLAT, height=10) lab1 = Label(self.toolbar, text="Width:", justify=RIGHT).pack(side=LEFT) self.tkWidthText = StringVar() self.tkWidthText.set(TT.BOARDWIDTH) bgcol = self.toolbar['bg'] #._root().cget('bg') Label(self.toolbar, textvariable=self.tkWidthText, width=3, padx=0, justify=LEFT, relief=FLAT).pack(side=LEFT) Label(self.toolbar, text=" Height:", justify=RIGHT).pack(side=LEFT) self.tkHeightText = StringVar() self.tkHeightText.set(TT.BOARDHEIGHT) Label(self.toolbar, textvariable=self.tkHeightText, width=3, padx=0, justify=LEFT, relief=FLAT).pack(side=LEFT) Label(self.toolbar, text=" Temp:", justify=RIGHT).pack(side=LEFT) self.tkTempText = StringVar() self.tkTempText.set(TT.TEMP) Label(self.toolbar, textvariable=self.tkTempText, width=3, padx=0, justify=LEFT, relief=FLAT).pack(side=LEFT) self.toolbar.pack(side=TOP, fill=X) self.mainframe.pack() #other class variables self.gridcolor = "#000000" self.textcolor = "#000000" self.drawgrid() self.CreateInitial()