def showAnimalInfoBox(self, imageName, xPosition, yPosition, name, age, gender, species): # checks if the label was pressed before or not - if it was pushed before the infolabel is # destroyed and the press counter is set to False if self.infoLabel is not None and ( self.lastAnimalPositionY != yPosition or self.lastAnimalPositionX != xPosition): self.pressedAnimalName = False #count presses, if first press true if second press false self.infoLabel.destroy() self.destroyAnimalInfo() #if pressedName is false (users first push is made) a messageLabel is created and pressedName changes to True if not self.pressedAnimalName: self.infoLabel = MyButton(imageName) self.infoLabel.addAsLabel(self.tkRoot, xPosition, yPosition) self.pressedAnimalName = True #if name contains something then information is sent to showAnimalInfo if name: self.showAnimalInfo(xPosition, yPosition, name, age, gender, species) else: #destroys labels if they contain information and resets attributes to None/False if self.infoLabel is not None: self.infoLabel.destroy() self.destroyAnimalInfo() self.infoLabel = None self.pressedAnimalName = False #checks wich button the user puched last time self.lastAnimalPositionY = yPosition self.lastAnimalPositionX = xPosition
def tipBuyAnimal(self, event): if self.tipLabelBuy != None: self.cancelTipBuy(None) self.tipLabelBuy = MyButton("tipLabel") self.tipLabelBuy.addAsLabel(self.tkRoot, 245, 215) #randomly collects a singel animal lonleyAnimalSpecies = str(self.checkForLonleyAnimal()) recommendedAnimal = str(self.checkGenderOfAnimal(lonleyAnimalSpecies)) #if there is one or more lonley animal shall a message be printed with the recommmended animal if recommendedAnimal != "no animal": self.textLabelBuy = Label(text = "We recommend you to \n buy a " + recommendedAnimal + " " + lonleyAnimalSpecies + "!", font = ("Myriad Pro", 20), fg = "white", bg ="#6fe577") self.textLabelBuy.place(x = 300, y = 300) #if there is no lonley animal a label with no tip will be shown else: self.textLabelNoAnimalBuy = Label(text = "None of your animals\n are lonley, " "buy which \nanimal you like", font = ("Myriad Pro", 20), fg = "white", bg ="#6fe577") self.textLabelNoAnimalBuy.place(x = 300, y = 290) self.cancelButtonTipBuy = MyButton("cancelButton") self.cancelButtonTipBuy.addAsLabel(self.tkRoot, 380, 410, self.cancelTipBuy)
def checkNumberOfAnimals(self, event): if len(self.viewDict["home"].animalList) <= 0: print(len(self.viewDict["home"].animalList)) self.tooFewAnimalsLabel = MyButton("tooFewAnimals") self.tooFewAnimalsLabel.addAsLabel(self.tkRoot, 245, 215) self.cancelTooFewAnimals = MyButton("cancelButton") self.cancelTooFewAnimals.addAsLabel(self.tkRoot, 380, 410, self.closeTooFewAnimals) else: self.sellAnimal()
def createButtonMenu(self): #Go back to main self.buttonMain = MyButton("backButton") self.buttonMain.addAsLabel(self.tkRoot, 107, 565, self.goToMain) #Buttons for buying, selling and getting tips self.buttonBuy = MyButton("buyAnimal") self.buttonBuy.addAsLabel(self.tkRoot, 298, 518, self.buyAnimal) self.buttonTipBuy = MyButton("tipBuy") self.buttonTipBuy.addAsLabel(self.tkRoot, 298, 591, self.tipBuyAnimal) self.buttonSell = MyButton("sellAnimal") self.buttonSell.addAsLabel(self.tkRoot, 435, 518, self.checkNumberOfAnimals) self.buttonTipSell = MyButton("tipSell") self.buttonTipSell.addAsLabel(self.tkRoot, 435, 591, self.tipSellAnimal)
def display(self): self.animalList = [] self.animalLabels = [] #create background image img = Image.open("polybackground.png") self.imgBackground = ImageTk.PhotoImage(img) #display background image on screen self.panel = Label(self.tkRoot, image=self.imgBackground, borderwidth=0) self.panel.pack(side="bottom", fill="both", expand="yes") #top button self.buttonTop = MyButton("animalButton") self.buttonTop.addAsLabel(self.tkRoot, 350, 50) self.createButtonMenu()
def buyAnimal(self, event): #if there is 35 or more animals in the zoo the user has to sell an animal before buying a new one if len(self.viewDict["home"].animalList) >= 35: #errorlabel if there is to many animals self.tooManyAnimalsLabel = MyButton("tooManyAnimals") self.tooManyAnimalsLabel.addAsLabel(self.tkRoot, 245, 215) self.cancelTooManyAnimals = MyButton("cancelButton") self.cancelTooManyAnimals.addAsLabel(self.tkRoot, 380, 410, self.closeTooManyAnimals) #Label where users can buy an animal by giving information about what they want to buy else: self.labelBuyInfo = MyButton("formBlock") self.labelBuyInfo.addAsLabel(self.tkRoot, 245, 215) self.labelNameEntry = Entry(bd = 0, width = 24, font = "14") self.labelNameEntry.place(x = 375, y = 245) self.labelAgeEntry = Entry(bd = 0, width = 24, font = "14") self.labelAgeEntry.place(x = 375, y = 281) self.labelSpeciesEntry = Entry(bd = 0, width = 24, font = "14") self.labelSpeciesEntry.place(x = 375, y = 353) self.radioVariabel = StringVar() self.radioVariabel.set("gender") self.radioFemale = Radiobutton(self.tkRoot, bg="#6fe577", activebackground="#6fe577", text="Female" , variable=self.radioVariabel, value="Female", indicatoron = 1) self.radioFemale.place(x = 375, y = 317) self.radioMale = Radiobutton(self.tkRoot, text="Male", bg="#6fe577", activebackground="#6fe577", variable=self.radioVariabel, value="Male", indicatoron = 1) self.radioMale.place(x = 440, y = 317) self.buyButton = MyButton("buyButton") self.buyButton.addAsLabel(self.tkRoot, 330, 410, self.checkInput) #Remove labels to buy an animal self.cancelButtonBuy = MyButton("cancelButton") self.cancelButtonBuy.addAsLabel(self.tkRoot, 435, 410, self.cancelBuy)
def showError(self, messageText, xPosition, yPosition, text=False): # checks if the label was pressed before or not - if it was pushed before the messagelabel is now # destroyed and the press counter is set to False if self.messageLabel is not None and self.lastPosition != yPosition: self.messageLabel.destroy() self.pressedName = False #count presses, if first press true if second press false if self.textLabelError is not None: self.textLabelError.destroy() #if pressedName is false (users first push is made) a messageLabel is created and pressedName changes to True if not self.pressedName: self.messageLabel = MyButton(messageText) self.messageLabel.addAsLabel(self.tkRoot, xPosition, yPosition) self.pressedName = True # if text-attribute contains something then a text shall appear if text == True: self.textLabelError = Label( text="Zebra, Parrot, Tiger, \nSnow leopard, Fox, Rabbit, " "\nAntilope, Elephant, Penguin \nLemur, Clownfish, T-rex \n " "Panda, Giraffe, Snake \n Serval, Car, Frog, Pig \n" "Horse, Sea star, Fire dragon, \nIce dragon and Unicorn", font=("Myriad Pro", 10), fg="white", bg="#6fe577") self.textLabelError.place(x=660, y=400) else: #destroys labels if they contain information and resets attributes to None/False if self.messageLabel is not None: self.messageLabel.destroy() if self.textLabelError is not None: self.textLabelError.destroy() self.textLabelError = None self.messageLabel = None self.pressedName = False #checks wich button the user puched last time self.lastPosition = yPosition
def tipSellAnimal(self, event): if self.tipLabelSell != None: self.cancelTipSell(None) self.tipLabelSell = MyButton("tipLabel") self.tipLabelSell.addAsLabel(self.tkRoot, 245, 215) recommendedAnimal = str(self.checkForLonleyAnimal()) #if there is one or more lonley animal shall a message be printed with the recommmended animal if recommendedAnimal != "no animal": self.textLabelSell = Label(text = "We recommend you to \n sell a " + recommendedAnimal + "!", font = ("Myriad Pro", 20), fg = "white", bg ="#6fe577") self.textLabelSell.place(x = 300, y = 300) #if there is no lonley animal shall a message about that be shown else: self.textLabelNoAnimalSell = Label(text = "None of your animals\n are lonley, " "sell which \nanimal you like", font = ("Myriad Pro", 20), fg = "white", bg ="#6fe577") self.textLabelNoAnimalSell.place(x = 300, y = 290) self.cancelButtonTipSell = MyButton("cancelButton") self.cancelButtonTipSell.addAsLabel(self.tkRoot, 380, 410, self.cancelTipSell)
def sellAnimal(self): self.labelSellAnimal = MyButton("sellName") self.labelSellAnimal.addAsLabel(self.tkRoot, 245, 215) #User writes the name of the animal that is to be sold self.sellEntry = Entry(bd = 0, width = 24, font = "14") self.sellEntry.place(x = 325, y = 350) self.sellButton = MyButton("sellButton") self.sellButton.addAsLabel(self.tkRoot, 330, 410, self.checkSoldAnimalName) self.cancelButtonSell = MyButton("cancelButton") self.cancelButtonSell.addAsLabel(self.tkRoot, 435, 410, self.cancelSell)
def createButtonMenu(self): #buy and sell button self.buttonBuy = MyButton("buySellButton") self.buttonBuy.addAsLabel(self.tkRoot, 350, 550, self.goToSellBuy) #search button self.buttonSearch = MyButton("searchButton") self.buttonSearch.addAsLabel(self.tkRoot, 171, 550, self.goToSearch) #sort button self.buttonSort = MyButton("sortButton") self.buttonSort.addAsLabel(self.tkRoot, 529, 550, self.goToSort)
def createButtonMenu(self): #back to main page self.buttonMain = MyButton("backButton") self.buttonMain.addAsLabel(self.tkRoot, 107, 565, self.goToMain) #Eight buttons doind different kinds of sorting self.buttonNameAZ = MyButton("namesAZ") self.buttonNameAZ.addAsLabel(self.tkRoot, 161, 518, self.nameAZ) self.buttonNameZA = MyButton("namesZA") self.buttonNameZA.addAsLabel(self.tkRoot, 161, 591, self.nameZA) self.buttonAgeYO = MyButton("ageYO") self.buttonAgeYO.addAsLabel(self.tkRoot, 298, 518, self.ageYoung) self.buttonAgeOY = MyButton("ageOY") self.buttonAgeOY.addAsLabel(self.tkRoot, 298, 591, self.ageOld) self.buttonGenderFM = MyButton("genderFM") self.buttonGenderFM.addAsLabel(self.tkRoot, 435, 518, self.genderFemale) self.buttonGenderMF = MyButton("genderMF") self.buttonGenderMF.addAsLabel(self.tkRoot, 435, 591, self.genderMale) self.buttonSpeciesAZ = MyButton("speciesAZ") self.buttonSpeciesAZ.addAsLabel(self.tkRoot, 572, 518, self.speciesAZ) self.buttonSpeciesZA = MyButton("speciesZA") self.buttonSpeciesZA.addAsLabel(self.tkRoot, 572, 591, self.speciesZA)
class ZooSort: def __init__(self, tkRoot, viewDict): self.tkRoot = tkRoot self.viewDict = viewDict def createButtonMenu(self): #back to main page self.buttonMain = MyButton("backButton") self.buttonMain.addAsLabel(self.tkRoot, 107, 565, self.goToMain) #Eight buttons doind different kinds of sorting self.buttonNameAZ = MyButton("namesAZ") self.buttonNameAZ.addAsLabel(self.tkRoot, 161, 518, self.nameAZ) self.buttonNameZA = MyButton("namesZA") self.buttonNameZA.addAsLabel(self.tkRoot, 161, 591, self.nameZA) self.buttonAgeYO = MyButton("ageYO") self.buttonAgeYO.addAsLabel(self.tkRoot, 298, 518, self.ageYoung) self.buttonAgeOY = MyButton("ageOY") self.buttonAgeOY.addAsLabel(self.tkRoot, 298, 591, self.ageOld) self.buttonGenderFM = MyButton("genderFM") self.buttonGenderFM.addAsLabel(self.tkRoot, 435, 518, self.genderFemale) self.buttonGenderMF = MyButton("genderMF") self.buttonGenderMF.addAsLabel(self.tkRoot, 435, 591, self.genderMale) self.buttonSpeciesAZ = MyButton("speciesAZ") self.buttonSpeciesAZ.addAsLabel(self.tkRoot, 572, 518, self.speciesAZ) self.buttonSpeciesZA = MyButton("speciesZA") self.buttonSpeciesZA.addAsLabel(self.tkRoot, 572, 591, self.speciesZA) def goToMain(self, event): #Destroy items self.buttonMain.destroy() self.buttonNameAZ.destroy() self.buttonNameZA.destroy() self.buttonAgeYO.destroy() self.buttonAgeOY.destroy() self.buttonGenderFM.destroy() self.buttonGenderMF.destroy() self.buttonSpeciesAZ.destroy() self.buttonSpeciesZA.destroy() #if the user have pushed on an animal picture it will be destroyed when moving to a different page self.viewDict["home"].destroyAnimalInfo() #resets pressedName to False if it was True if self.viewDict["home"].pressedAnimalName: self.viewDict["home"].pressedAnimalName = False self.viewDict["home"].createButtonMenu() def nameAZ(self, event): #returns animal with A (or first) first in list sortedList = sorted(self.viewDict["home"].animalList, key=lambda animal: animal.name.lower()) animalList = sortedList self.viewDict["home"].updateAnimalList(animalList) self.viewDict["home"].destroyAnimalInfo() def nameZA(self, event): #returns animal with Z (or last) first in list sortedList = sorted(self.viewDict["home"].animalList, key=lambda animal: animal.name.lower(), reverse=True) animalList = sortedList self.viewDict["home"].updateAnimalList(animalList) self.viewDict["home"].destroyAnimalInfo() def ageYoung(self, event): #returns youngest animal first in list sortedList = sorted(self.viewDict["home"].animalList, key=lambda animal: animal.age) animalList = sortedList self.viewDict["home"].updateAnimalList(animalList) self.viewDict["home"].destroyAnimalInfo() def ageOld(self, event): #returns oldest animal first in list sortedList = sorted(self.viewDict["home"].animalList, key=lambda animal: animal.age, reverse=True) animalList = sortedList self.viewDict["home"].updateAnimalList(animalList) self.viewDict["home"].destroyAnimalInfo() def genderFemale(self, event): #returns femalesbfirst in list sortedList = sorted(self.viewDict["home"].animalList, key=lambda animal: animal.gender.lower()) animalList = sortedList self.viewDict["home"].updateAnimalList(animalList) self.viewDict["home"].destroyAnimalInfo() def genderMale(self, event): #returns malesbfirst in list sortedList = sorted(self.viewDict["home"].animalList, key=lambda animal: animal.gender.lower(), reverse=True) animalList = sortedList self.viewDict["home"].updateAnimalList(animalList) self.viewDict["home"].destroyAnimalInfo() def speciesAZ(self, event): #returns animal with A (or first) first in list sortedList = sorted(self.viewDict["home"].animalList, key=lambda animal: animal.species.lower()) animalList = sortedList self.viewDict["home"].updateAnimalList(animalList) self.viewDict["home"].destroyAnimalInfo() def speciesZA(self, event): #returns animal with Z (or last) first in list sortedList = sorted(self.viewDict["home"].animalList, key=lambda animal: animal.species.lower(), reverse=True) animalList = sortedList self.viewDict["home"].updateAnimalList(animalList) self.viewDict["home"].destroyAnimalInfo()
def createButtonMenu(self): #go back to main self.buttonMain = MyButton("backButton") self.buttonMain.addAsLabel(self.tkRoot, 117, 565, self.goToMain) #search name self.labelName = MyButton("searchName") self.labelName.addAsLabel(self.tkRoot, 171, 518) #search age self.labelAge = MyButton("searchAge") self.labelAge.addAsLabel(self.tkRoot, 171, 557) #search gender self.labelGender = MyButton("searchGender") self.labelGender.addAsLabel(self.tkRoot, 171, 596) #search species self.labelSpecies = MyButton("searchSpecies") self.labelSpecies.addAsLabel(self.tkRoot, 171, 635) #User input for search self.labelNameEntry = Entry(bd = 0, width = 67) self.labelAgeEntry = Entry(bd = 0, width = 67) self.labelGenderEntry = Entry(bd = 0, width = 67) self.labelSpeciesEntry = Entry(bd = 0, width = 67) entryList = [self.labelNameEntry, self.labelAgeEntry, self.labelGenderEntry, self.labelSpeciesEntry] #place for entry for index, entry in enumerate(entryList): entry.place(x = 282, y = 528 + 38.5*index) #Search self.buttonSearch = MyButton("searchSearch") self.buttonSearch.addAsLabel(self.tkRoot, 700, 565, self.checkSearch)
def noMatch(self): self.noMatchImg = MyButton("noMatches") self.noMatchImg.addAsLabel(self.tkRoot, 245, 215) self.closeButton = MyButton("closeButton") self.closeButton.addAsLabel(self.tkRoot, 360, 350, self.goToDestroy)
class ZooSearch: def __init__(self, tkRoot, viewDict): self.tkRoot = tkRoot self.viewDict = viewDict def createButtonMenu(self): #go back to main self.buttonMain = MyButton("backButton") self.buttonMain.addAsLabel(self.tkRoot, 117, 565, self.goToMain) #search name self.labelName = MyButton("searchName") self.labelName.addAsLabel(self.tkRoot, 171, 518) #search age self.labelAge = MyButton("searchAge") self.labelAge.addAsLabel(self.tkRoot, 171, 557) #search gender self.labelGender = MyButton("searchGender") self.labelGender.addAsLabel(self.tkRoot, 171, 596) #search species self.labelSpecies = MyButton("searchSpecies") self.labelSpecies.addAsLabel(self.tkRoot, 171, 635) #User input for search self.labelNameEntry = Entry(bd = 0, width = 67) self.labelAgeEntry = Entry(bd = 0, width = 67) self.labelGenderEntry = Entry(bd = 0, width = 67) self.labelSpeciesEntry = Entry(bd = 0, width = 67) entryList = [self.labelNameEntry, self.labelAgeEntry, self.labelGenderEntry, self.labelSpeciesEntry] #place for entry for index, entry in enumerate(entryList): entry.place(x = 282, y = 528 + 38.5*index) #Search self.buttonSearch = MyButton("searchSearch") self.buttonSearch.addAsLabel(self.tkRoot, 700, 565, self.checkSearch) def goToMain(self, event): #Destroy items listOfItems = [self.buttonMain, self.labelName, self.labelAge, self.labelGender, self.labelSpecies, self.buttonSearch, self.labelAgeEntry, self.labelNameEntry, self.labelGenderEntry, self.labelSpeciesEntry] for item in listOfItems: if item is not None: item.destroy() self.viewDict["home"].destroyAnimalInfo() if self.viewDict["home"].pressedAnimalName: self.viewDict["home"].pressedAnimalName = False for animLabel in self.viewDict["home"].animalLabels: animLabel.img = Image.open("square.png") animLabel.tkImg = ImageTk.PhotoImage(animLabel.img) animLabel.imgLabel.configure(image = animLabel.tkImg) animLabel.imgLabel.image = animLabel.tkImg self.viewDict["home"].createButtonMenu() def checkSearch(self, event): #gather and make all input in lowercase userName = self.labelNameEntry.get().lower() userAge = self.labelAgeEntry.get().lower() userGender = self.labelGenderEntry.get().lower() userSpecies = self.labelSpeciesEntry.get().lower() matchingAnimals = [] for animal in self.viewDict["home"].animalList: matchName = False matchAge = False matchGender = False matchSpecies = False #if no input - no matches if userName == "" and userAge == "" and userGender == "" and userSpecies == "": self.noMatch() self.goToDestroy(None) else: #see what user input matches zoo-animals if userName == "" or userName == animal.name.lower(): matchName = True if userAge == "" or userAge == str(animal.age): matchAge = True if userGender == "" or userGender == animal.gender.lower(): matchGender = True if userSpecies == "" or userSpecies == animal.species.lower(): matchSpecies = True #if match - add animal to a list of matches if matchName == True and matchAge == True and matchGender == True and matchSpecies == True: matchingAnimals.append(animal) #Checks if there is a match or not lengthOfMatchList = len(matchingAnimals) if lengthOfMatchList != 0: self.greenSearchLabel(matchingAnimals) else: self.noMatch() #changes background image from black to green when matching def greenSearchLabel(self, listOfMatches): for animLabel in self.viewDict["home"].animalLabels: #makes all labels black animLabel.img = Image.open("square.png") animLabel.tkImg = ImageTk.PhotoImage(animLabel.img) animLabel.imgLabel.configure(image = animLabel.tkImg) animLabel.imgLabel.image = animLabel.tkImg for animLabel in self.viewDict["home"].animalLabels: #matches animal with animallabel and chages the color of the label for animal in listOfMatches: if animLabel.anAnimal == animal: newImg = Image.open("greenSquare.png") newTkImg = ImageTk.PhotoImage(newImg) animLabel.imgLabel.configure(image = newTkImg) animLabel.imgLabel.image = newTkImg animLabel.img = newImg animLabel.tkImg = newTkImg #shows label when there is no match with user input and animalList def noMatch(self): self.noMatchImg = MyButton("noMatches") self.noMatchImg.addAsLabel(self.tkRoot, 245, 215) self.closeButton = MyButton("closeButton") self.closeButton.addAsLabel(self.tkRoot, 360, 350, self.goToDestroy) #removes noMatch content def goToDestroy(self, event): self.noMatchImg.destroy() self.closeButton.destroy()
class ZooHome: def __init__(self, tkRoot, viewDict): self.tkRoot = tkRoot #makes size of the window fixed self.tkRoot.resizable(width=False, height=False) self.tkRoot.title("Zoo") self.viewDict = viewDict self.pressedName = False self.pressedAnimalName = False self.lastPosition = 0 self.lastAnimalPositionY = 0 self.lastAnimalPositionX = 0 self.infoLabel = None self.messageLabel = None self.textLabelError = None self.nameText = None self.ageText = None self.genderText = None self.speciesText = None self.messageLabel = None #creates first page with buttons and background image def display(self): self.animalList = [] self.animalLabels = [] #create background image img = Image.open("polybackground.png") self.imgBackground = ImageTk.PhotoImage(img) #display background image on screen self.panel = Label(self.tkRoot, image=self.imgBackground, borderwidth=0) self.panel.pack(side="bottom", fill="both", expand="yes") #top button self.buttonTop = MyButton("animalButton") self.buttonTop.addAsLabel(self.tkRoot, 350, 50) self.createButtonMenu() def createButtonMenu(self): #buy and sell button self.buttonBuy = MyButton("buySellButton") self.buttonBuy.addAsLabel(self.tkRoot, 350, 550, self.goToSellBuy) #search button self.buttonSearch = MyButton("searchButton") self.buttonSearch.addAsLabel(self.tkRoot, 171, 550, self.goToSearch) #sort button self.buttonSort = MyButton("sortButton") self.buttonSort.addAsLabel(self.tkRoot, 529, 550, self.goToSort) #Go to sell/buy page def goToSellBuy(self, event): self.goToOtherPage("buySell") #Go to search page def goToSearch(self, event): self.goToOtherPage("search") #Go to sort page def goToSort(self, event): self.goToOtherPage("sort") #destroys button menu and sends the user to the desired page def goToOtherPage(self, page): self.buttonBuy.destroy() self.buttonSort.destroy() self.buttonSearch.destroy() #destroys animalInfo the info is on the screen self.destroyAnimalInfo() if self.pressedAnimalName: self.pressedAnimalName = False self.viewDict[page].createButtonMenu() #reads in animals from file def readAnimalsFromFile(self): #open list of animals self.animalList = [] animalFile = open("animalList.txt", "r") #appends all the animals to a list for row in animalFile: if len(row) == 1: break else: splitRow = row.split(", ") name = splitRow[0] age = int(splitRow[1]) species = splitRow[2] gender = splitRow[3].replace("\n", "") #create animal instance animalInstance = Animal(name, age, species, gender) self.animalList.append(animalInstance) #Updates animalList before adding animals to window def updateAnimalList(self, newAnimalList): self.animalList = newAnimalList self.addAnimalsToWindow() #create grid of animals def addAnimalsToWindow(self): self.animalLabels = [] index = 0 animalCount = len(self.animalList) #squares in 2d-list for each animal for column in range(140, 480, 75): for row in range(171, 650, 75): if index < animalCount: #black squares if no animal anAnimal = self.animalList[index] animalLabel = AnimalLabel(anAnimal, "square.png") animalLabel.addAsLabel(self.tkRoot, row, column, self.createLambda( row, column, anAnimal.name, anAnimal.age, anAnimal.gender, anAnimal.species)) #event that # occurs when user pusches the # label. Sends info about animal # to createLambda self.animalLabels.append(animalLabel) index += 1 else: #creates images of animals animalLabel = AnimalLabel(None, "square.png") animalLabel.addAsLabel(self.tkRoot, row, column) self.animalLabels.append(animalLabel) #captures all values passed in as arguments and creates a lambda that calls showInfoBox #with the captured values def createLambda(self, row, column, name, age, gender, species): return lambda x: self.showInfoBox(row, column, name, age, gender, species) #function that create different error message labels that can be pusched once to be shown and twice to be deleted def showError(self, messageText, xPosition, yPosition, text=False): # checks if the label was pressed before or not - if it was pushed before the messagelabel is now # destroyed and the press counter is set to False if self.messageLabel is not None and self.lastPosition != yPosition: self.messageLabel.destroy() self.pressedName = False #count presses, if first press true if second press false if self.textLabelError is not None: self.textLabelError.destroy() #if pressedName is false (users first push is made) a messageLabel is created and pressedName changes to True if not self.pressedName: self.messageLabel = MyButton(messageText) self.messageLabel.addAsLabel(self.tkRoot, xPosition, yPosition) self.pressedName = True # if text-attribute contains something then a text shall appear if text == True: self.textLabelError = Label( text="Zebra, Parrot, Tiger, \nSnow leopard, Fox, Rabbit, " "\nAntilope, Elephant, Penguin \nLemur, Clownfish, T-rex \n " "Panda, Giraffe, Snake \n Serval, Car, Frog, Pig \n" "Horse, Sea star, Fire dragon, \nIce dragon and Unicorn", font=("Myriad Pro", 10), fg="white", bg="#6fe577") self.textLabelError.place(x=660, y=400) else: #destroys labels if they contain information and resets attributes to None/False if self.messageLabel is not None: self.messageLabel.destroy() if self.textLabelError is not None: self.textLabelError.destroy() self.textLabelError = None self.messageLabel = None self.pressedName = False #checks wich button the user puched last time self.lastPosition = yPosition #shows errorlabel if user enters wrong characters def errorName(self, event): self.showError("alphaError", 632, 241) def errorAge(self, event): self.showError("ageError", 632, 277) def errorGender(self, event): self.showError("genderError", 632, 313) def errorSpecies(self, event): self.showError("speciesError", 632, 349, True) #Sends info about animal to showMessage def showInfoBox(self, row, column, name, age, gender, species): self.showAnimalInfoBox("infoLabel", int(row) - 35, int(column) + 70, name, age, gender, species) #keeps track of the info boxes for each animal and checks if the infobox should be shown or not def showAnimalInfoBox(self, imageName, xPosition, yPosition, name, age, gender, species): # checks if the label was pressed before or not - if it was pushed before the infolabel is # destroyed and the press counter is set to False if self.infoLabel is not None and ( self.lastAnimalPositionY != yPosition or self.lastAnimalPositionX != xPosition): self.pressedAnimalName = False #count presses, if first press true if second press false self.infoLabel.destroy() self.destroyAnimalInfo() #if pressedName is false (users first push is made) a messageLabel is created and pressedName changes to True if not self.pressedAnimalName: self.infoLabel = MyButton(imageName) self.infoLabel.addAsLabel(self.tkRoot, xPosition, yPosition) self.pressedAnimalName = True #if name contains something then information is sent to showAnimalInfo if name: self.showAnimalInfo(xPosition, yPosition, name, age, gender, species) else: #destroys labels if they contain information and resets attributes to None/False if self.infoLabel is not None: self.infoLabel.destroy() self.destroyAnimalInfo() self.infoLabel = None self.pressedAnimalName = False #checks wich button the user puched last time self.lastAnimalPositionY = yPosition self.lastAnimalPositionX = xPosition #if user pushes an animalLabel this function makes a box of information about the animal pop up def showAnimalInfo(self, xPosition, yPosition, name, age, gender, species): name = self.makeFirstCharacterUppercase(name) gender = self.makeFirstCharacterUppercase(gender) species = self.makeFirstCharacterUppercase(species) #create textlabels with animal information self.nameText = Label(text=str(name), font=("Myriad Pro", 12), fg="white", bg="#6fe577") self.nameText.place(x=xPosition + 56, y=yPosition + 9) self.ageText = Label(text=str(age), font=("Myriad Pro", 12), fg="white", bg="#6fe577") self.ageText.place(x=xPosition + 56, y=yPosition + 28) self.genderText = Label(text=str(gender), font=("Myriad Pro", 12), fg="white", bg="#6fe577") self.genderText.place(x=xPosition + 68, y=yPosition + 48) self.speciesText = Label(text=str(species), font=("Myriad Pro", 12), fg="white", bg="#6fe577") self.speciesText.place(x=xPosition + 68, y=yPosition + 66) #make first character in string uppercase def makeFirstCharacterUppercase(self, word): word = word[0].upper() + word[1:] return word #destroys infolabels about animals if they contain information def destroyAnimalInfo(self): listOfItems = [ self.nameText, self.ageText, self.genderText, self.speciesText, self.infoLabel ] for item in listOfItems: if item is not None: item.destroy()
def noMatchName(self): self.errorNameSellButton = MyButton("error") self.errorNameSellButton.addAsLabel(self.tkRoot, 550, 345, self.errorSellName)
def checkInput(self, event): #delete earlier errorlabels if they contain some information errorButtons = [self.errorNameButton, self.errorSpeciesButton, self.errorGenderButton, self.errorAgeButton] for button in errorButtons: if button is not None: button.destroy() #collect user data userName = self.labelNameEntry.get() userAge = self.labelAgeEntry.get() userGender = self.radioVariabel.get() userSpecies = self.labelSpeciesEntry.get() checkedName = self.checkName(userName) #if checkedName is false if not checkedName: self.errorNameButton = MyButton("error") self.errorNameButton.addAsLabel(self.tkRoot, 600, 241, self.viewDict["home"].errorName) #check if age is integer try: userAge = int(userAge) #check age checkedAge = self.checkAge(userAge) if not checkedAge: self.errorAgeButton = MyButton("error") self.errorAgeButton.addAsLabel(self.tkRoot, 600, 277, self.viewDict["home"].errorAge) else: checkedAge = True except: self.errorAgeButton = MyButton("error") self.errorAgeButton.addAsLabel(self.tkRoot, 600, 277, self.viewDict["home"].errorAge) checkedAge = False #Check gender checkedGender = self.checkGender(userGender) if not checkedGender: self.errorGenderButton = MyButton("error") self.errorGenderButton.addAsLabel(self.tkRoot, 600, 313, self.viewDict["home"].errorGender) else: checkedGender = True #check species checkedSpecies = self.checkSpecies(userSpecies) if not checkedSpecies: self.errorSpeciesButton = MyButton("error") self.errorSpeciesButton.addAsLabel(self.tkRoot, 600, 349, self.viewDict["home"].errorSpecies) else: checkedSpecies = True #if all information is true - create animal if checkedName == True and checkedAge == True and checkedGender == True and checkedSpecies == True: #adds new animal to list newAnimalToList = [userName, int(userAge), userSpecies, userGender] #removes special characters from string and writes to file stringAnimal = str(newAnimalToList) stringAnimal = stringAnimal.replace("'", "") stringAnimal = stringAnimal.strip("[") stringAnimal = stringAnimal.strip("]") file = open("animalList.txt", "a") file.write(stringAnimal + "\n") file.close() #destroys buylabels and adds new animals to window self.cancelBuy(event) self.viewDict["home"].readAnimalsFromFile() self.viewDict["home"].addAnimalsToWindow()
class ZooBuySell: def __init__(self, tkRoot, viewDict): self.tkRoot = tkRoot self.viewDict = viewDict self.errorNameButton = None self.errorSpeciesButton = None self.errorGenderButton = None self.errorAgeButton = None self.labelNameEntry = None self.labelAgeEntry = None self.labelGenderEntry = None self.labelSpeciesEntry = None self.textLabelSell = None self.textLabelNoAnimalSell = None self.textLabelBuy = None self.textLabelNoAnimalBuy = None self.errorNameSellButton = None self.tipLabelBuy = None self.tipLabelSell = None def createButtonMenu(self): #Go back to main self.buttonMain = MyButton("backButton") self.buttonMain.addAsLabel(self.tkRoot, 107, 565, self.goToMain) #Buttons for buying, selling and getting tips self.buttonBuy = MyButton("buyAnimal") self.buttonBuy.addAsLabel(self.tkRoot, 298, 518, self.buyAnimal) self.buttonTipBuy = MyButton("tipBuy") self.buttonTipBuy.addAsLabel(self.tkRoot, 298, 591, self.tipBuyAnimal) self.buttonSell = MyButton("sellAnimal") self.buttonSell.addAsLabel(self.tkRoot, 435, 518, self.checkNumberOfAnimals) self.buttonTipSell = MyButton("tipSell") self.buttonTipSell.addAsLabel(self.tkRoot, 435, 591, self.tipSellAnimal) #Go back to mainpage def goToMain(self, event): #Destroy items if they contain information listOfItems = [self.buttonMain, self.buttonBuy, self.buttonSell, self.buttonTipBuy, self.buttonTipSell] for item in listOfItems: if item is not None: item.destroy() self.viewDict["home"].destroyAnimalInfo() if self.viewDict["home"].pressedAnimalName: self.viewDict["home"].pressedAnimalName = False self.viewDict["home"].createButtonMenu() #Buy animal def buyAnimal(self, event): #if there is 35 or more animals in the zoo the user has to sell an animal before buying a new one if len(self.viewDict["home"].animalList) >= 35: #errorlabel if there is to many animals self.tooManyAnimalsLabel = MyButton("tooManyAnimals") self.tooManyAnimalsLabel.addAsLabel(self.tkRoot, 245, 215) self.cancelTooManyAnimals = MyButton("cancelButton") self.cancelTooManyAnimals.addAsLabel(self.tkRoot, 380, 410, self.closeTooManyAnimals) #Label where users can buy an animal by giving information about what they want to buy else: self.labelBuyInfo = MyButton("formBlock") self.labelBuyInfo.addAsLabel(self.tkRoot, 245, 215) self.labelNameEntry = Entry(bd = 0, width = 24, font = "14") self.labelNameEntry.place(x = 375, y = 245) self.labelAgeEntry = Entry(bd = 0, width = 24, font = "14") self.labelAgeEntry.place(x = 375, y = 281) self.labelSpeciesEntry = Entry(bd = 0, width = 24, font = "14") self.labelSpeciesEntry.place(x = 375, y = 353) self.radioVariabel = StringVar() self.radioVariabel.set("gender") self.radioFemale = Radiobutton(self.tkRoot, bg="#6fe577", activebackground="#6fe577", text="Female" , variable=self.radioVariabel, value="Female", indicatoron = 1) self.radioFemale.place(x = 375, y = 317) self.radioMale = Radiobutton(self.tkRoot, text="Male", bg="#6fe577", activebackground="#6fe577", variable=self.radioVariabel, value="Male", indicatoron = 1) self.radioMale.place(x = 440, y = 317) self.buyButton = MyButton("buyButton") self.buyButton.addAsLabel(self.tkRoot, 330, 410, self.checkInput) #Remove labels to buy an animal self.cancelButtonBuy = MyButton("cancelButton") self.cancelButtonBuy.addAsLabel(self.tkRoot, 435, 410, self.cancelBuy) #destroys error label def closeTooManyAnimals(self, event): self.tooManyAnimalsLabel.destroy() self.cancelTooManyAnimals.destroy() #checks if the user has written in correct data about the animal def checkInput(self, event): #delete earlier errorlabels if they contain some information errorButtons = [self.errorNameButton, self.errorSpeciesButton, self.errorGenderButton, self.errorAgeButton] for button in errorButtons: if button is not None: button.destroy() #collect user data userName = self.labelNameEntry.get() userAge = self.labelAgeEntry.get() userGender = self.radioVariabel.get() userSpecies = self.labelSpeciesEntry.get() checkedName = self.checkName(userName) #if checkedName is false if not checkedName: self.errorNameButton = MyButton("error") self.errorNameButton.addAsLabel(self.tkRoot, 600, 241, self.viewDict["home"].errorName) #check if age is integer try: userAge = int(userAge) #check age checkedAge = self.checkAge(userAge) if not checkedAge: self.errorAgeButton = MyButton("error") self.errorAgeButton.addAsLabel(self.tkRoot, 600, 277, self.viewDict["home"].errorAge) else: checkedAge = True except: self.errorAgeButton = MyButton("error") self.errorAgeButton.addAsLabel(self.tkRoot, 600, 277, self.viewDict["home"].errorAge) checkedAge = False #Check gender checkedGender = self.checkGender(userGender) if not checkedGender: self.errorGenderButton = MyButton("error") self.errorGenderButton.addAsLabel(self.tkRoot, 600, 313, self.viewDict["home"].errorGender) else: checkedGender = True #check species checkedSpecies = self.checkSpecies(userSpecies) if not checkedSpecies: self.errorSpeciesButton = MyButton("error") self.errorSpeciesButton.addAsLabel(self.tkRoot, 600, 349, self.viewDict["home"].errorSpecies) else: checkedSpecies = True #if all information is true - create animal if checkedName == True and checkedAge == True and checkedGender == True and checkedSpecies == True: #adds new animal to list newAnimalToList = [userName, int(userAge), userSpecies, userGender] #removes special characters from string and writes to file stringAnimal = str(newAnimalToList) stringAnimal = stringAnimal.replace("'", "") stringAnimal = stringAnimal.strip("[") stringAnimal = stringAnimal.strip("]") file = open("animalList.txt", "a") file.write(stringAnimal + "\n") file.close() #destroys buylabels and adds new animals to window self.cancelBuy(event) self.viewDict["home"].readAnimalsFromFile() self.viewDict["home"].addAnimalsToWindow() #check if user has written a correct name def checkName(self, userName): matchingCharacter = False lengthOfName = len(userName) # if two animals have the same name - set new name to nothing (which will make the name incorrect) for animal in self.viewDict["home"].animalList: if animal.name.lower() == userName.lower(): userName = "" # checks if user has written anything at all or if the name short enough, if not the name is incorrect if userName != "" and lengthOfName < 11: lowerName = userName.lower() characterList = "abcdefghijklmnopqrstuvwxyz" firstCharacter = lowerName[0] # checks if the name starts with an alphabetic character, if not the name is incorrect for char in characterList: if char == firstCharacter: matchingCharacter = True return matchingCharacter return matchingCharacter # checks if the user has written in a propper age def checkAge(self, userAge): if len(str(userAge)) >= 11 or userAge < 0: return False else: return True # check if users written gender is correct def checkGender(self, userGender): genderStatus = None lowerGender = userGender.lower() if lowerGender == "female" or lowerGender == "male": if lowerGender == "female": genderStatus = "female" return genderStatus if lowerGender == "male": genderStatus = "male" return genderStatus return genderStatus #check if users written species is in listOfSpecies def checkSpecies(self, userSpecies): speciesList = None lowerSpecies = userSpecies.lower() listOfSpecies = ["tiger", "snow leopard", "t-rex", "rabbit", "fox", "parrot", "penguin", "elephant", "lemur", "antilope", "clownfish", "zebra", "unicorn", "panda", "giraffe", "snake", "serval", "fire dragon", "ice dragon", "cat", "frog", "pig", "horse", "sea star"] for animal in listOfSpecies: if animal == lowerSpecies: speciesList = animal return speciesList return speciesList #removes labels from Buy-function def cancelBuy(self, event): listOfItems = [self.labelAgeEntry, self.labelNameEntry, self.labelSpeciesEntry, self.radioFemale, self.radioMale, self.buyButton, self.cancelButtonBuy, self.labelBuyInfo] for item in listOfItems: item.destroy() errorButtons = [self.errorNameButton, self.errorSpeciesButton, self.errorGenderButton, self.errorAgeButton, self.viewDict["home"].textLabelError, self.viewDict["home"].messageLabel] for button in errorButtons: if button is not None: button.destroy() #Tip for buying def tipBuyAnimal(self, event): if self.tipLabelBuy != None: self.cancelTipBuy(None) self.tipLabelBuy = MyButton("tipLabel") self.tipLabelBuy.addAsLabel(self.tkRoot, 245, 215) #randomly collects a singel animal lonleyAnimalSpecies = str(self.checkForLonleyAnimal()) recommendedAnimal = str(self.checkGenderOfAnimal(lonleyAnimalSpecies)) #if there is one or more lonley animal shall a message be printed with the recommmended animal if recommendedAnimal != "no animal": self.textLabelBuy = Label(text = "We recommend you to \n buy a " + recommendedAnimal + " " + lonleyAnimalSpecies + "!", font = ("Myriad Pro", 20), fg = "white", bg ="#6fe577") self.textLabelBuy.place(x = 300, y = 300) #if there is no lonley animal a label with no tip will be shown else: self.textLabelNoAnimalBuy = Label(text = "None of your animals\n are lonley, " "buy which \nanimal you like", font = ("Myriad Pro", 20), fg = "white", bg ="#6fe577") self.textLabelNoAnimalBuy.place(x = 300, y = 290) self.cancelButtonTipBuy = MyButton("cancelButton") self.cancelButtonTipBuy.addAsLabel(self.tkRoot, 380, 410, self.cancelTipBuy) #checks which genders the recommended animalspecies has and recommends the gender that is "lonley" def checkGenderOfAnimal(self, recommendedSpecies): listOfAnimals = [] for animal in self.viewDict["home"].animalList: if recommendedSpecies == animal.species: listOfAnimals.append(animal) males = 0 females = 0 for animalGender in listOfAnimals: if animalGender.gender.lower() == "female": females += 1 if animalGender.gender.lower() == "male": males += 1 if males > females: return "female" else: return "male" #tip for selling def tipSellAnimal(self, event): if self.tipLabelSell != None: self.cancelTipSell(None) self.tipLabelSell = MyButton("tipLabel") self.tipLabelSell.addAsLabel(self.tkRoot, 245, 215) recommendedAnimal = str(self.checkForLonleyAnimal()) #if there is one or more lonley animal shall a message be printed with the recommmended animal if recommendedAnimal != "no animal": self.textLabelSell = Label(text = "We recommend you to \n sell a " + recommendedAnimal + "!", font = ("Myriad Pro", 20), fg = "white", bg ="#6fe577") self.textLabelSell.place(x = 300, y = 300) #if there is no lonley animal shall a message about that be shown else: self.textLabelNoAnimalSell = Label(text = "None of your animals\n are lonley, " "sell which \nanimal you like", font = ("Myriad Pro", 20), fg = "white", bg ="#6fe577") self.textLabelNoAnimalSell.place(x = 300, y = 290) self.cancelButtonTipSell = MyButton("cancelButton") self.cancelButtonTipSell.addAsLabel(self.tkRoot, 380, 410, self.cancelTipSell) #makes all speices to lowercase characters def getSpeciesOf(self, animal): return animal.species.lower() #checks if there is a lonley animal def checkForLonleyAnimal(self): listOfLonleyAnimals = [] #collects all species from animallist speciesList = map(self.getSpeciesOf, self.viewDict["home"].animalList) #counts how many of each animals is in the specieslist countAnimals = collections.Counter(speciesList) #if the number of animals is a modulus of two - add animal to listOfLonleyAnimals for animal, numberOfAnimal in countAnimals.items(): #returns a tuple containing animal: numberOfAnimals if numberOfAnimal%2 == 1: listOfLonleyAnimals.append(animal) lengthOfLonleyAnimals = len(listOfLonleyAnimals) #if listOfLonleyAnimals doesent contain something - return "no animal" if not listOfLonleyAnimals: noAnimal = "no animal" return noAnimal #if listOfLonleyAnimals contains something - take randomly out one of them to be shown for the user else: randomAnimalIndex = randint(0, lengthOfLonleyAnimals-1) return listOfLonleyAnimals[randomAnimalIndex] #removes labels from tipSell-function def cancelTipSell(self, event): self.tipLabelSell.destroy() self.cancelButtonTipSell.destroy() textLabels = [self.textLabelSell, self.textLabelNoAnimalSell] for labels in textLabels: if labels is not None: labels.destroy() #removes labels from tipBuy-function def cancelTipBuy(self, event): self.tipLabelBuy.destroy() self.cancelButtonTipBuy.destroy() textLabels = [self.textLabelBuy, self.textLabelNoAnimalBuy] for labels in textLabels: if labels is not None: labels.destroy() def checkNumberOfAnimals(self, event): if len(self.viewDict["home"].animalList) <= 0: print(len(self.viewDict["home"].animalList)) self.tooFewAnimalsLabel = MyButton("tooFewAnimals") self.tooFewAnimalsLabel.addAsLabel(self.tkRoot, 245, 215) self.cancelTooFewAnimals = MyButton("cancelButton") self.cancelTooFewAnimals.addAsLabel(self.tkRoot, 380, 410, self.closeTooFewAnimals) else: self.sellAnimal() def closeTooFewAnimals(self, event): self.tooFewAnimalsLabel.destroy() self.cancelTooFewAnimals.destroy() #Sell animal def sellAnimal(self): self.labelSellAnimal = MyButton("sellName") self.labelSellAnimal.addAsLabel(self.tkRoot, 245, 215) #User writes the name of the animal that is to be sold self.sellEntry = Entry(bd = 0, width = 24, font = "14") self.sellEntry.place(x = 325, y = 350) self.sellButton = MyButton("sellButton") self.sellButton.addAsLabel(self.tkRoot, 330, 410, self.checkSoldAnimalName) self.cancelButtonSell = MyButton("cancelButton") self.cancelButtonSell.addAsLabel(self.tkRoot, 435, 410, self.cancelSell) #removes labels from sell-function def checkSoldAnimalName(self, event): foundAnimal = False userName = self.sellEntry.get() lowerName = userName.lower() #check if the users written name matches with an animal in animalList for animal in self.viewDict["home"].animalList: if animal.name.lower() == lowerName: foundAnimal = True self.subFromAnimalList(lowerName) if foundAnimal == False: self.noMatchName() #error message if no animal match the users name def noMatchName(self): self.errorNameSellButton = MyButton("error") self.errorNameSellButton.addAsLabel(self.tkRoot, 550, 345, self.errorSellName) def errorSellName(self, event): self.showError("nameError", 580, 345) def subFromAnimalList(self, animalName): #open file with animals and read lines to fileLines file = open("animalList.txt", "r") fileLines = file.readlines() file.close #open file and for each line read in the animals name and check for each line if the name of the animal that # will be removed matches with the lines in the file. If true then the animal is removed and the # other ones moved up file = open("animalList.txt", "w" ) for line in fileLines: splitRow = line.split(", ") lowerSplit = splitRow[0].lower() if lowerSplit != animalName.lower(): file.write(line) file.close() self.cancelSell(lowerSplit) self.viewDict["home"].readAnimalsFromFile() self.viewDict["home"].updateAnimalList(self.viewDict["home"].animalList) def cancelSell(self, event): self.sellEntry.destroy() self.sellButton.destroy() self.cancelButtonSell.destroy() self.labelSellAnimal.destroy() textLabels = [self.errorNameSellButton, self.viewDict["home"].messageLabel] for labels in textLabels: if labels is not None: labels.destroy()