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): #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 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 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 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 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 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 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 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 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)
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()