##CSC323 ##The GUI for filling and editing a board from tkinter import * from ButtonCreator import * import BoardScreen import Menu #Code to import ../model/ files import sys, socket sys.path.append('../model') sys.path.append('../network') import Species import LocalWorld from SpeciesDatabase import * import worldserver import client def run(event = None): """Creates and runs the setup""" root = Tk() BoardSetup = SetupFrame(root) root.mainloop() class SetupFrame: """Create and fill the Frame of the setup menu""" def __init__(self, parent, width=480, height=600): 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="ZeroLife Setup",fg="black") title.pack(fill=X) self.container = OptionContainer(self.parent) class OptionContainer: """Creates a frame to contain the setup options.""" 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.addOptions() self.addLowerButtons() def addOptions(self): """Add boxes for options to the frame.""" self.addWidthBox() self.addHeightBox() self.addPortBox() self.speciesSelector() self.networkCheck() def addWidthBox(self): """Add the width text entry field""" self.widthContainer = Frame(self.container, width=10, bg="darkblue") widthLabel = Label(self.widthContainer, text="Enter a Width: ",\ bg = "darkblue", fg = "white") widthLabel.pack(fill=Y, side=LEFT) self.widthEntry = Entry(self.widthContainer) #default value is 10 self.widthEntry.insert(0,"10") self.widthEntry.pack(fill=Y, side=LEFT) self.widthContainer.pack(pady = 30) def addHeightBox(self): """Add the height text entry field""" self.heightContainer = Frame(self.container, width=10, bg="darkblue") heightLabel = Label(self.heightContainer, text="Enter a Height: ",\ bg = "darkblue", fg = "white") heightLabel.pack(fill=Y, side=LEFT) self.heightEntry = Entry(self.heightContainer) #Default value is 10 self.heightEntry.insert(0,"10") self.heightEntry.pack(fill=Y, side=LEFT) self.heightContainer.pack(pady = 30) def addPortBox(self): """The port used for networking""" self.portContainer = Frame(self.container, width=10, bg="darkblue") portLabel = Label(self.portContainer, text="Port: ", \ bg = "darkblue", fg = "white") portLabel.pack(fill=Y, side=LEFT) self.portEntry = Entry(self.portContainer) #Default port is 32323 self.portEntry.insert(0,"32323") self.portEntry.pack(fill=Y, side=LEFT) self.portContainer.pack() def speciesSelector(self): """Add the species selector listbox""" self.selectorFrame = Frame(self.container, width=10, bg="darkblue",\ pady=25) speciesLabel = Label(self.selectorFrame, text="Select a default species: ",\ bg = "darkblue", fg = "white") speciesLabel.pack(side=TOP, fill=BOTH, expand=YES) self.speciesList = Listbox(self.selectorFrame, height=5) """Creates scrollbar""" scroll = Scrollbar(self.selectorFrame) scroll.pack(side = RIGHT, fill = Y) scroll.config(command = self.speciesList.yview) '''SpeciesDatabase access, will (probably) need to change.''' for species in self.specdb.allSpeciesNames(): self.speciesList.insert(END, species) self.speciesList.config(yscrollcommand = scroll.set) self.speciesList.pack(side=TOP) self.selectorFrame.pack(side=TOP) def networkCheck(self): """net returns 0 if local, 1 if starting network game, 2 if joining""" self.net = IntVar() self.net.set(0) self.networkCheckContainer = Frame(self.container, width=10,\ bg="darkblue") labels = ["Play locally", "Start networked game", "Join networked game"] for la in labels: networkCheckButton = \ ButtonCreator(self.networkCheckContainer, la, radio = 1, radioval = labels.index(la),\ radiovar = self.net, radioind = 1) self.networkCheckContainer.pack(side=TOP) def addLowerButtons(self): """Add Buttons to the container""" self.lowerButtonFrame = Frame(self.container, bg="darkblue") startGameButton = ButtonCreator(self.lowerButtonFrame, "Start",\ self.getInputs, packOption = RIGHT) mainMenuButton = ButtonCreator(self.lowerButtonFrame, "Main Menu",\ self.spawnMenu, packOption = LEFT) self.lowerButtonFrame.pack(side=BOTTOM, fill = BOTH) def getInputs(self, event): """Collects the active inputs in the fields before submitting""" w = int(self.widthEntry.get()) h = int(self.heightEntry.get()) port = int(self.portEntry.get()) net = self.net.get() defSpec = self.specdb.getSpecies(self.speciesList.get(ACTIVE)) defCol = defSpec.color print("The radio button choice was: " + str(net)) if net == 0: lw = LocalWorld.LocalWorld(defSpec, w, h) self.parent.destroy() BoardScreen.run(w, h, defSpec, defCol, lw) elif net == 1: lw = worldserver.WorldServer(port, w, h, defSpec) self.parent.destroy() BoardScreen.run(w, h, defSpec, defCol, lw) elif net == 2: self.parent.destroy() lw = client.WorldClient() #find some way to get the desired hostname/port #just get the current hostname and assume 32323 for now hostname = socket.gethostname() lw.connect(hostname, port) BoardScreen.run(lw.ws.x, lw.ws.y, lw.ws.default, lw.ws.default.color, lw) #if self.net.get() != 0: # do network stuff instead #lw = LocalWorld.LocalWorld(defSpec, w, h) #BoardScreen.run(w, h,defSpec,defCol, lw) def spawnMenu(self, event = None): """Loads the Main Menu and destroys this window""" self.parent.destroy() Menu.run(event)