So basically as a beginner programmer I created this program that calculates maximum possible evolves for different pokemon/candy combinations in pokemon go using Tkinter. I created this program before just using a function with al the code. However, I thought it would be good to use a class and after some research this was the result. However, I basically got it working by adding self to a whole bunch of variables in the different methods.
My question is: Is the use of a class even relevant here. My research on classes involved populating a class with objects etc, but here i just create 1 object in order to use the class and run a bunch of calculations.
Furthermore, how do I know when to use self when calling a variable. For instance I create static class variables with the impression that i don't have to put the instance self in front of it to use it, but that didn't work so I just put more selfs in front of the calling of the variable. Why does it work to put a bunch of selfs in front of everything? Are the selfs overkill and could I do it in some smarter way?
from tkinter import *
from tkinter import messagebox
class PokeCalculator(Frame):
pokedex = {
'pidgey': 12,
'caterpie': 12,
'weedle': 12,
'rattata': 25
}
choices = pokedex.keys()
screen_title = 'Pokemon evolve calculator'
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.make_window()
def make_window(self):
self.master.title(self.screen_title)
L1 = Label(self, text='Candies')
L1.grid(column=1, row=0)
L2 = Label(self, text='Pokemon amount in storage')
L2.grid(column=2, row=0)
self.var = StringVar()
self.var.set('Pokemon')
self.Pokemon = OptionMenu(self, self.var, *self.choices)
self.Pokemon.grid(column=0, row=1)
self.Candies = Entry(self)
self.Candies.grid(column=1, row=1)
self.Poke_amount = Entry(self)
self.Poke_amount.grid(column=2, row=1)
Calculate = Button(self, text='Calculate', command=self.get_and_check)
Calculate.grid(column=1, row=2)
def get_and_check(self):
self.get_values()
self.check_input()
def get_values(self):
self.poke = self.var.get()
self.candies_total = self.Candies.get()
self.p_amount = self.Poke_amount.get()
def check_input(self):
string1 = 'Please select a Pokemon from the dropdown menu'
string2 = 'Please only enter numbers'
if self.poke == 'Pokemon':
messagebox.showinfo(self.screen_title, string1)
elif not self.p_amount.isdigit() or not self.candies_total.isdigit():
messagebox.showinfo(self.screen_title, string2)
else:
self.too_few_pokemon()
def too_few_pokemon(self):
candies_total = int(self.candies_total)
p_amount = int(self.p_amount)
evolve = int((candies_total - 1) / (self.pokedex[self.poke] - 1))
candies_needed = (p_amount * (self.pokedex[self.poke] - 1)) + 1
if p_amount <= evolve:
n = 0
while candies_needed <= candies_total:
n = n + 1
p_amount = p_amount + 1
candies_needed = ((p_amount) * (self.pokedex[self.poke] - 1)) + 1
candies_total = candies_total + 3
evolve2 = int((candies_total - 1) / (self.pokedex[self.poke] - 1))
string1 = ''' You have enough candies too evolve {0} {1},
but you only have {2} {1} in storage and thus can only
evolve {2} {1}.
If you catch {3} more {1} you can now evolve {4} {1}.'''
messagebox.showinfo(self.screen_title, string1.format(evolve, self.poke,
self.p_amount,
n, evolve2))
else:
self.too_much_pokemon()
def too_much_pokemon(self):
candies_total = int(self.candies_total)
p_amount = int(self.p_amount)
candies_needed = (p_amount * (self.pokedex[self.poke] - 1)) + 1
m = 0
while candies_total <= candies_needed:
m = m + 1
p_amount = p_amount - 1
candies_needed = ((p_amount) * (self.pokedex[self.poke] - 1)) + 1
candies_total = candies_total + 1
evolve = int((candies_total - 1) / (self.pokedex[self.poke] - 1))
string2 = 'Transfer {0} {1} so you can evolve a total of {2} {1}.'
messagebox.showinfo(self.screen_title, string2.format(m, self.poke, evolve))
root = Tk()
app = PokeCalculator(root)
app.pack()
root.mainloop()