##April 2010 ##CSC323 ##The GUI for modifying or creating a species database. from tkinter import * from ButtonCreator import * #Code to import ../model/ files import sys sys.path.append('../model') import Species import SpeciesDatabase import Menu import SpeciesSetup def run(event = None): """Creates and runs the setup""" root = Tk() SpeciesSetup = SetupFrame(root) root.mainloop() class SetupFrame: """Create and fill the Frame of the selection menu""" def __init__(self, parent, width=480, height=600): self.parent = parent self.specdb = SpeciesDatabase.getInstance() self.parent.geometry(str(width)+"x"+str(height)) self.frame = Frame(self.parent, width=width, height=height) self.frame.pack() title = Label(self.frame, text="Species Database",fg="black") title.pack(fill=X) self.container = ShowSpecies(self.parent) class ShowSpecies: """Creates a frame to display the species database.""" def __init__(self, parent, width = 2000, bg = "darkblue"): width = width bg = bg self.container = Frame(parent, width=width, bg=bg) self.container.pack(side = BOTTOM, fill = BOTH, expand = YES) self.parent = parent self.specdb = SpeciesDatabase.getInstance() self.showDatabase() self.addLowerButtons() self.addLoadingFrame() def showDatabase(self): """Add the species database display""" self.listContainer = Frame(self.container, width=10, bg="darkblue") listLabel = Label(self.listContainer, text="Species Database",\ bg = "darkblue", fg = "white") listLabel.pack(fill=Y, side=TOP) self.speciesList = Listbox(self.listContainer, height=20) """Creates scrollbar""" scroll = Scrollbar(self.listContainer) scroll.pack(side = RIGHT, fill = Y) scroll.config(command = self.speciesList.yview) l = self.specdb.allSpeciesNames() for name in l: self.speciesList.insert(END, name) self.speciesList.insert(END, "New Species") self.speciesList.config(yscrollcommand = scroll.set) self.speciesList.pack(side=TOP) self.listContainer.pack(side=TOP, pady=30) def addLowerButtons(self): """Add Buttons to the container""" self.lowerButtonFrame = Frame(self.container, bg="darkblue") modifyButton = ButtonCreator(self.lowerButtonFrame, "Modify/New",\ self.modifySpecies, packOption = RIGHT) mainMenuButton = ButtonCreator(self.lowerButtonFrame, "Main Menu",\ self.spawnMenu, packOption = LEFT) self.lowerButtonFrame.pack(side=BOTTOM, fill = BOTH) def addLoadingFrame(self): self.loadingFrame = Frame(self.container, bg="darkblue") self.filenameField = Entry(self.loadingFrame) self.filenameField.pack(side=LEFT,fill=BOTH) loadButton=ButtonCreator(self.loadingFrame,"Load File",self.loadFile\ ,packOption=RIGHT) self.loadingFrame.pack(side=BOTTOM, fill= BOTH) def loadFile(self, event= None): """Loads species from a species database file""" self.specdb.loadFile(self.filenameField.get()) self.speciesList.delete(0,self.speciesList.size()-1) l = self.specdb.allSpeciesNames() for name in l: self.speciesList.insert(END, name) self.speciesList.insert(END, "New Species") def modifySpecies(self, event = None): """Enters species setup for the selected species""" specname = self.speciesList.get(ACTIVE) self.parent.destroy() if specname != "New Species": core = self.specdb.getSpecies(specname) print(core.countRules()) SpeciesSetup.run(spec = core, dbsbsize = core.countRules()) else: SpeciesSetup.run() def spawnMenu(self, event = None): """Loads the Main Menu and destroys this window""" self.parent.destroy() Menu.run()