##April 2010 ##CSC323 ##The GUI for modifying or creating a species. from tkinter import * from ButtonCreator import * #Code to import ../model/ files import sys sys.path.append('../model') import Species import LocalWorld import SpeciesDatabase import Menu import BoardScreen import ShowSpecies import RuleEditor def run(spec = None, dbsbsize = 0, event = None): """Creates and runs the setup""" root = Tk() SpeciesSetup = SetupFrame(root, spec, dbsbsize) root.mainloop() class SetupFrame: """Create and fill the Frame of the setup menu""" def __init__(self, parent, spec, dbsbsize, width=480, height=800): self.parent = parent 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 Setup",fg="black") title.pack(fill=X) self.container = OptionContainer(self.parent, spec, dbsbsize) class OptionContainer: """Creates a frame to contain the setup options.""" def __init__(self, parent, spec, dbsbsize, 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.dbsbsize = dbsbsize """Optional spec parameter allows modification of existing species""" self.spec = spec """Holds lists of rules to remove when save is clicked""" self.removeRules = [] self.addOptions() def addOptions(self): """Add options.""" self.addNameBox() self.addColorBox() self.ruleBox() self.addStrengths() self.addLowerButtons() def addNameBox(self): """Add the name text entry field""" self.nameContainer = Frame(self.container, width=8, bg="darkblue") nameLabel = Label(self.nameContainer, text="Species name: ",\ bg = "darkblue", fg = "white") nameLabel.pack(fill=Y, side=LEFT) self.nameEntry = Entry(self.nameContainer) if self.spec == None: self.nameEntry.insert(0, "Default") else: self.nameEntry.insert(0, self.spec.kind) self.nameEntry.pack(fill=Y, side=LEFT) self.nameContainer.pack(padx=10, pady=30) def addColorBox(self): """Add the color text entry field""" self.colorContainer = Frame(self.container, width=8, bg="darkblue") colorLabel = Label(self.colorContainer, text="Species color: ",\ bg = "darkblue", fg = "white") colorLabel.pack(fill=Y, side=LEFT) self.colorEntry = Entry(self.colorContainer) if self.spec == None: self.colorEntry.insert(0, "purple") else: self.colorEntry.insert(0, self.spec.color) self.colorEntry.pack(fill=Y, side=BOTTOM) """Adds list of valid tkinter colors""" self.colorContainer.pack(side=TOP, padx=10, pady=30) def ruleBox(self): """Add the listbox displaying current rules""" self.ruleFrame = Frame(self.container, width=200, bg="darkblue",\ pady=25) ruleLabel = Label(self.ruleFrame, text="Species Rules",\ bg = "darkblue", fg = "white") ruleLabel.pack(side=TOP, fill=BOTH, expand=YES) self.listFrame = Frame(self.ruleFrame, width=200, bg="darkblue",\ pady=5) self.listFrame.pack(side = TOP) self.ruleList = Listbox(self.listFrame, height=5, width = 100) """Creates x and y scrollbar""" yscrollbar = Scrollbar(self.listFrame) yscrollbar.pack(side = RIGHT, fill = Y) yscrollbar.config(command = self.ruleList.yview) xscrollbar = Scrollbar(self.listFrame) xscrollbar.pack(side = BOTTOM, fill = X) xscrollbar.config(command=self.ruleList.xview, orient = HORIZONTAL) self.ruleDict = {} """Add the rules to RuleList""" if self.spec != None: for r in self.spec.rules: if r !=None: self.ruleDict[str(r)] = r self.ruleList.insert(END, str(r)) self.ruleList.config(yscrollcommand = yscrollbar.set,\ xscrollcommand = xscrollbar.set) self.ruleList.pack(side=TOP) ButtonCreator(self.ruleFrame, "Add New Rule", self.newRule,\ packOption = BOTTOM) ButtonCreator(self.ruleFrame, "Remove Rule", self.removeRule,\ packOption = BOTTOM) ButtonCreator(self.ruleFrame, "Edit Rule", self.editRule,\ packOption = BOTTOM) self.ruleFrame.pack() def addStrengths(self): """Checkboxes to select species you are stronger than""" strengthFrame = Frame(self.container, bg = "darkblue") strengthFrame.pack(padx = 5, pady = 5) Label(strengthFrame,bg = "darkblue", fg = "white",\ text = "Select species you are STRONGER than").pack(side = TOP) """Add checkbuttons for all other species, store vars in strength""" self.strength = {} for s in self.specdb.allSpeciesNames(): if self.specdb.species[s] != self.spec: self.strength[s]= IntVar() Checkbutton(strengthFrame, text=s, variable =\ self.strength[s]).pack(side = LEFT, padx = 5) """Add a checkbutton for overwrite""" self.overwrite = IntVar() Checkbutton(strengthFrame, text = "Overwrite old strength pairings?",\ variable = self.overwrite).pack(side = RIGHT, padx = 5) def addLowerButtons(self): """Add Buttons to the container""" self.lowerButtonFrame = Frame(self.container, bg="darkblue") self.lowerButtonFrame.pack(side=BOTTOM, fill = BOTH) ButtonCreator(self.lowerButtonFrame, "Save",\ self.saveSpecies, packOption = RIGHT, pady = 5) ButtonCreator(self.lowerButtonFrame, "Cancel",\ self.cancel, packOption = RIGHT, pady = 5) def saveSpecies(self, event): """Collects the active inputs in the fields before submitting""" name = self.nameEntry.get() color = self.colorEntry.get() """We get and create the list of weaker species""" lstronger = [] lweaker = [] for s in self.strength.items(): if s[1].get() == 1: lstronger.append(self.specdb.species[s[0]]) else: lweaker.append(self.specdb.species[s[0]]) if self.spec == None: """If we are modifying a new species, create it""" self.spec = Species.Species(name,color) elif self.spec.kind in self.specdb.species: """Checks if species is already in the database, removes if true""" self.specdb.species.pop(self.spec.kind) self.specdb.speciesByColor.pop(self.spec.color) self.spec.kind = name self.spec.color = color """Add the revised species to the database""" self.specdb.addSpecies(self.spec,strongerList = lstronger, weakerList = lweaker, ow = self.overwrite) """Now, we remove the stored list of rules to remove from self.spec""" for r in self.removeRules: self.spec.rules.remove(r) self.parent.destroy() ShowSpecies.run() def cancel(self, event = None): """Loads the Species Menu and destroys this window""" '''Uses the dbsbsize count floating around to revert the database''' while self.spec.countRules() > self.dbsbsize: self.spec.rulePop() self.parent.destroy() ShowSpecies.run() def editRule(self, event, new = 0): """Calls RuleEditor with existing or new rule""" if new == 1: rule = None else: rule = self.ruleDict[self.ruleList.get(ACTIVE)] if self.spec == None: self.spec = Species.Species("Default", "purple") self.parent.destroy() RuleEditor.run(self.specdb, self.spec, self.dbsbsize, rule) def newRule(self, event): """Goes to RuleEditor to create a new rule""" self.editRule(event, 1) def removeRule(self, event): """Removes the active listbox rule from species""" rule = self.ruleDict[self.ruleList.get(ACTIVE)] self.removeRules.append(rule) self.ruleList.delete(ANCHOR) def killRule(self, rule, event): """Removes any rule that exists from species""" print(self.ruleList(1)) pass