#Group2 Nathan, Denny, Dylan, Jeffrey #April 12 #CSC 323 #The basic GUI for the Game of life #GROUP 2 Nathan, Denny, Dylan, Jeffrey from tkinter import * class GraphicsUI: #"""GUI for single user mode, x and y are ints that describe width and height. defaultSpecies is the color that all the #grid squres will be set to when the grid is built""" def __init__(self, parent1, x, y, defaultSpecies): self.parent = parent1 def run(self): #sets and initial size of the parent geometry parent1.geometry("600x400") #creates two containers to organize the other frames self.con_myContainerLeft = Frame(parent1,width=15,bg="tan") self.con_myContainerRight = Frame(parent1, width=2000,\ bg="orange") self.parent1 = parent1 #packs the two frames in parent self.con_myContainerLeft.pack(side = LEFT, fill = Y, expand = NO) self.con_myContainerRight.pack(side = LEFT, fill = BOTH, expand = YES) ##Begin Menu toolbar #builds a frame and packs it for the tool bar self.frm_ButtonLayout = Frame(self.con_myContainerLeft) self.frm_ButtonLayout.pack(side=TOP, fill=BOTH, expand=YES) #build and pack the options button self.btn_Options = Button(self.frm_ButtonLayout, text="Options",width=9) self.btn_Options.pack() #build and pack the credits button self.btn_Credits = Button(self.frm_ButtonLayout,text="Credits",width=9) self.btn_Credits.pack() #build and pack the main menu button self.btn_MainMenu = Button(self.frm_ButtonLayout, text="Main Menu",\ width=9) #bind the main menu button to the main menu frame #this still doesn't actually work self.btn_MainMenu.bind("", self.MainMenu) self.btn_MainMenu.pack() #bind the exit button to destory the app self.btn_Exit=Button(self.frm_ButtonLayout, text="Exit",width=9) self.btn_Exit.bind("", self.exitbutton) self.btn_Exit.pack() ##End Menu toolbar ##Begin ToolBar #build and pack a frame for the tool bar self.frm_ToolBar=Frame(self.con_myContainerLeft) self.frm_ToolBar.pack() #make an instance variable that keeps track of the color of the #previous specious button that was pressed self.color_prevButton= None #frame to hold the buttons for species types self.frm_SpeciesButtons=Frame(self.frm_ToolBar) self.frm_SpeciesButtons.pack() #some species buttons to put into the SpeciesFrame #each species button comes with a command that tells the #color_prevButton what color to change to self.btn_Blank=Button(self.frm_SpeciesButtons, text="Blank",\ background="white",width=10,\ command = self.btn_BlankClick) self.btn_Blank.pack(side=TOP) self.btn_Student=Button(self.frm_SpeciesButtons,text="Student",\ background="lightblue",width=10,\ command = self.btn_StudentClick) self.btn_Student.pack(side=TOP) self.btn_Professor=Button(self.frm_SpeciesButtons,width=10,\ text="Professor",background="red",\ command = self.btn_ProfessorClick) self.btn_Professor.pack(side=TOP) self.btn_Squirrel=Button(self.frm_SpeciesButtons,\ text="Squirrel",width=10,\ background="green",\ command = self.btn_SquirrelClick) self.btn_Squirrel.pack(side=TOP) self.btn_FM=Button(self.frm_SpeciesButtons,text="FM",width=10,\ background="yellow",\ command = self.btn_FMClick) self.btn_FM.pack(side=TOP) #frame to hold the zoom buttons #eventually we should figure out how to get little magnifying #glass icons self.frm_ZoomButtons=Frame(self.frm_ToolBar) self.frm_ZoomButtons.pack(side=BOTTOM) #zoom buttons self.btn_ZoomIn=Button(self.frm_ZoomButtons,text="Zoom In") self.btn_ZoomIn.pack(side=LEFT) self.btn_ZoomOut=Button(self.frm_ZoomButtons,text="Zoom Out") self.btn_ZoomOut.pack(side=LEFT) ##End Toolbar ##Begin Nathan's VCR # self.con_VCR = VCRControls.VCRControls(self.con_myContainerRight) self.frm_ButtonLayout = Frame(self.con_myContainerRight, height = 50) self.frm_ButtonLayout.pack(side=BOTTOM, fill = X) self.btn_Stop = Button(self.frm_ButtonLayout, text = "Stop", width = 8) self.btn_Step = Button(self.frm_ButtonLayout, text = "Step", width = 8) self.btn_Play = Button(self.frm_ButtonLayout, text = "Play", width = 8) self.scl_Speed = Scale(self.frm_ButtonLayout, from_=0, to = 100,\ orient = HORIZONTAL, length = 300,\ label = "Speed", width = 10) self.scl_Speed.pack(side = BOTTOM) self.btn_Play.pack(side = LEFT, padx = 20) self.btn_Step.pack(side = LEFT, padx = 20) self.btn_Stop.pack(side = LEFT, padx = 20) ##End Nathan's VCR ##the Grid #an array that holds all of the buttons self.cell = [[None for j in range(x)] for i in range(y)] #make a canvas to pack the cells into self.cnvs_cell_holder = Canvas(self.con_myContainerRight,\ bg = "white",width=600,\ height=400,scrollregion=(0,0,1200,800)) self.cnvs_cell_holder.pack(side = BOTTOM, fill = BOTH,\ expand = YES) self.createCells(x,y, defaultSpecies) #creates a grid of buttons with the given dimensions inside of #the canvas inside of grid container def createCells(self, x, y, defaultSpecies): i = 0 while(i < x): j = 0 while(j < y): #each button is connected with the GridClick method that will #set it's color when it is clicked on self.btn_base = Button(self.cnvs_cell_holder,\ bg= defaultSpecies,\ activebackground = "black") self.cell[i][j]=(self.btn_base) self.btn_base.grid(column=i,row=j) def handler(event, i=i, j=j): return self.btn_GridClick(event, i, j) self.btn_base.bind(sequence="",\ func=handler) j += 1 i += 1 #method to redraw the grid with updated creatures #as the method is written, it will accept a string name for the color #of the creature and turn that button into that color def update_board(self, board): """ i = 0 while(i < x): j = 0 while(j < y): self.cell[i][j].configure(bg=self.localWorld.getCreature(i,j)) j++ i++ """ pass def exitbutton(self,event): root.destroy() quit() def MainMenu(self, event): root.destroy() #import imp #imp.reload(MainMenu) import MainMenu quit() #methods that deal with species button clicks by assigning the correct #color to color_prevButton def btn_BlankClick(self): self.color_prevButton="white" def btn_StudentClick(self): self.color_prevButton="lightblue" def btn_ProfessorClick(self): self.color_prevButton="red" def btn_SquirrelClick(self): self.color_prevButton="green" def btn_FMClick(self): self.color_prevButton="yellow" #method to color the grid buttons the color_prevButton #method should also call the setCreature method on local world def btn_GridClick(self, event, i, j): event.widget.configure(bg=self.color_prevButton) print("i="+i,"j="+j) #self.localWorld.setCreature(i,j,self.color_prevButton) root = Tk() whatever = GraphicsUI(root, 20, 20, "white") root.wm_title("The Life of a Grinnalien") root.mainloop()