Memory Leek, critique me. Thanks!!!

K

Kevin McKinley

# I posted a few days ago about a memory leak that I think i'm having with my first Tkinter program.
# I've had trouble pinpointing what is wrong so i thought i would submit the code and see if anyone would
# like to critique it.
# I have one more problem that i can't figure out either. There are a bunch of Entry widgets that require number values.
# My problem is if someone inputs a letter or space the program will have error that makes part of the
# Program nonfunctional.
# Description Of The Program: I made this program to help me with common calculations that i do on a day-to-day
# basis. I'm a outside sales rep and this program figures out Gross profit and price increase. Oh, and where it
# said to enter COG that stands for Cost of Goods. I know it doesn't really matter but figure i would let you guys
# in on the use of the program.

# Thank you,
# Kevin McKinley




from Tkinter import *

class MyApp:
def __tini__(self, parent):
self.myParent = parent

self.myFunctionContainer = Frame(parent)
self.myFunctionContainer.grid(row=0, column=0)

self.COG1 = DoubleVar()
self.COG2 = DoubleVar()
self.COG3 = DoubleVar()
self.COG4 = DoubleVar()
self.GP = DoubleVar()
self.increase = DoubleVar()
self.markup = DoubleVar()

self.gpRange(1)


# This Function is for the Markup Tab for a range of GP%
def gpRange(self, event):
self.clearScreen()

self.gpRangeTab = Button(self.myFunctionContainer, width=14, relief=FLAT, text="Multi-Markup")
self.gpRangeTab.bind("<Button-1>", self.gpRange)
self.gpRangeTab.grid(row=0, column=0)

self.gpExactTab = Button(self.myFunctionContainer, width=14, bg="dark grey", relief=SUNKEN, text="Single Markup")
self.gpExactTab.bind("<Button-1>", self.gpExact)
self.gpExactTab.grid(row=0, column=1)

self.priceIncreaseTab = Button(self.myFunctionContainer, width=14, bg="dark grey", relief=SUNKEN, text="Product Increase")
self.priceIncreaseTab.bind("<Button-1>", self.priceIncrease)
self.priceIncreaseTab.grid(row=0, column=2)

self.gpFinderTab = Button(self.myFunctionContainer, width=14, bg="dark grey", relief=SUNKEN, text="Calculate GP")
self.gpFinderTab.bind("<Button-1>", self.gpFinder)
self.gpFinderTab.grid(row=0, column=3)

self.markup1(1)

self.myContainer2 = Frame(self.myFunctionContainer, borderwidth=2, relief=GROOVE)
self.myContainer2.grid(padx=0, pady=2, row=4, column=0, columnspan=4, sticky=W+E)

self.title1 = Label(self.myFunctionContainer, font="bold", relief=GROOVE, text="Multi-Markup Calculator")
self.title1.grid(padx=0, pady=2, row=1, column=0, columnspan=4, sticky=W+E)

self.entry1 = Entry(self.myFunctionContainer, textvariable=self.COG1)
self.entry1.select_range(0, END)
self.entry1.bind("<Return>", self.markup1)
self.entry1.focus_force()
self.entry1.grid(padx=2, pady=2, row=2, column=2, columnspan=2, sticky=W)

self.entryTitle = Label(self.myFunctionContainer, text="Enter COG:")
self.entryTitle.grid(padx=2, pady=2, row=2, column=1, sticky=E)

self.title2 = Label(self.myContainer2, text="Gross Profit % ")
self.title2.grid(padx=2, pady=2, row=1, column=1, sticky=W)

self.title3 = Label(self.myContainer2, text=" Markup Price")
self.title3.grid(padx=2, pady=2, row=1, column=2, sticky=E)

self.button1 = Button(self.myFunctionContainer, text="Calculate")
self.button1.bind("<Button-1>", self.markup1)
self.button1.grid(padx=2, pady=2, row=3, column=1, columnspan=2, sticky=W+E)


def gpExact(self, event):
self.clearScreen()

self.gpRangeTab = Button(self.myFunctionContainer, width=14, bg="dark grey", relief=SUNKEN, text="Multi-Markup")
self.gpRangeTab.bind("<Button-1>", self.gpRange)
self.gpRangeTab.grid(row=0, column=0)

self.gpExactTab = Button(self.myFunctionContainer, width=14, relief=FLAT, text="Single Markup")
self.gpExactTab.bind("<Button-1>", self.gpExact)
self.gpExactTab.grid(row=0, column=1)

self.priceIncreaseTab = Button(self.myFunctionContainer, width=14, bg="dark grey", relief=SUNKEN, text="Product Increase")
self.priceIncreaseTab.bind("<Button-1>", self.priceIncrease)
self.priceIncreaseTab.grid(row=0, column=2)

self.gpFinderTab = Button(self.myFunctionContainer, width=14, bg="dark grey", relief=SUNKEN, text="Calculate GP")
self.gpFinderTab.bind("<Button-1>", self.gpFinder)
self.gpFinderTab.grid(row=0, column=3)

self.markup2(1)

self.title1 = Label(self.myFunctionContainer, font="bold", relief=GROOVE, text="Single Markup Calculator")
self.title1.grid(padx=0, pady=2, row=1, column=0, columnspan=4, sticky=W+E)

self.entry1 = Entry(self.myFunctionContainer, textvariable=self.COG2)
self.entry1.select_range(0, END)
self.entry1.focus_force()
self.entry1.grid(padx=2, pady=2, row=2, column=2, columnspan=2, sticky=W)

self.entry2 = Entry(self.myFunctionContainer, textvariable=self.GP)
self.entry2.select_range(0, END)
self.entry2.bind("<Return>",self.markup2)
self.entry2.grid(padx=2, pady=2, row=3, column=2, columnspan=2, sticky=W)

self.entryTitle = Label(self.myFunctionContainer, text="Enter COG:")
self.entryTitle.grid(padx=2, pady=2, row=2, column=1, sticky=E)

self.entryTitle2 = Label(self.myFunctionContainer, text="Enter GP%:")
self.entryTitle2.grid(padx=2, pady=2, row=3, column=1, sticky=E)

self.button = Button(self.myFunctionContainer, text="Calculate")
self.button.bind("<Button-1>", self.markup2)
self.button.grid(padx=2, pady=2, row=4, column=1, columnspan=2, sticky=W+E)

def priceIncrease(self, event):
self.clearScreen()

self.gpRangeTab = Button(self.myFunctionContainer, width=14, bg="dark grey", relief=SUNKEN, text="Multi-Markup")
self.gpRangeTab.bind("<Button-1>", self.gpRange)
self.gpRangeTab.grid(row=0, column=0)

self.gpExactTab = Button(self.myFunctionContainer, width=14, bg="dark grey", relief=SUNKEN, text="Single Markup")
self.gpExactTab.bind("<Button-1>", self.gpExact)
self.gpExactTab.grid(row=0, column=1)

self.priceIncreaseTab = Button(self.myFunctionContainer, width=14, relief=FLAT, text="Product Increase")
self.priceIncreaseTab.bind("<Button-1>", self.priceIncrease)
self.priceIncreaseTab.grid(row=0, column=2)

self.gpFinderTab = Button(self.myFunctionContainer, width=14, bg="dark grey", relief=SUNKEN, text="Calculate GP")
self.gpFinderTab.bind("<Button-1>", self.gpFinder)
self.gpFinderTab.grid(row=0, column=3)

self.increase1(1)

self.title1 = Label(self.myFunctionContainer, font="bold", relief=GROOVE, text="Product Price Increase")
self.title1.grid(padx=0, pady=2, row=1, column=0, columnspan=4, sticky=W+E)

self.entry1 = Entry(self.myFunctionContainer, textvariable=self.COG3)
self.entry1.select_range(0, END)
self.entry1.focus_force()
self.entry1.grid(padx=2, pady=2, row=2, column=2, columnspan=2, sticky=W)

self.entry2 = Entry(self.myFunctionContainer, textvariable=self.increase)
self.entry2.select_range(0, END)
self.entry2.bind("<Return>", self.increase1)
self.entry2.grid(padx=2, pady=2, row=3, column=2, columnspan=2, sticky=W)

self.entryTitle = Label(self.myFunctionContainer, text="Enter COG:")
self.entryTitle.grid(padx=2, pady=2, row=2, column=1, sticky=E)

self.entryTitle2 = Label(self.myFunctionContainer, text="Enter Increase %:")
self.entryTitle2.grid(padx=2, pady=2, row=3, column=1, sticky=E)

self.button = Button(self.myFunctionContainer, text="Calculate")
self.button.bind("<Button-1>", self.increase1)
self.button.grid(padx=2, pady=2, row=4, column=1, columnspan=2, sticky=W+E)

def gpFinder(self, event):
self.clearScreen()

self.gpRangeTab = Button(self.myFunctionContainer, width=14, bg="dark grey",relief=SUNKEN, text="Multi-Markup")
self.gpRangeTab.bind("<Button-1>", self.gpRange)
self.gpRangeTab.grid(row=0, column=0)

self.gpExactTab = Button(self.myFunctionContainer, width=14, bg="dark grey", relief=SUNKEN, text="Single Markup")
self.gpExactTab.bind("<Button-1>", self.gpExact)
self.gpExactTab.grid(row=0, column=1)

self.priceIncreaseTab = Button(self.myFunctionContainer, width=14, bg="dark grey", relief=SUNKEN, text="Product Increase")
self.priceIncreaseTab.bind("<Button-1>", self.priceIncrease)
self.priceIncreaseTab.grid(row=0, column=2)

self.gpFinderTab = Button(self.myFunctionContainer, width=14, relief=FLAT, text="Calculate GP")
self.gpFinderTab.bind("<Button-1>", self.gpFinder)
self.gpFinderTab.grid(row=0, column=3)

self.gpCalc1(1)

self.title1 = Label(self.myFunctionContainer, font="bold", relief=GROOVE, text="GP Finder")
self.title1.grid(padx=0, pady=2, row=1, column=0, columnspan=4, sticky=W+E)

self.entry1 = Entry(self.myFunctionContainer, textvariable=self.COG4)
self.entry1.select_range(0, END)
self.entry1.focus_force()
self.entry1.grid(padx=2, pady=2, row=2, column=2, columnspan=2, sticky=W)

self.entry2 = Entry(self.myFunctionContainer, textvariable=self.markup)
self.entry2.select_range(0, END)
self.entry2.bind("<Return>", self.gpCalc1)
self.entry2.grid(padx=2, pady=2, row=3, column=2, columnspan=2, sticky=W)

self.entryTitle = Label(self.myFunctionContainer, text="Enter COG:")
self.entryTitle.grid(padx=2, pady=2, row=2, column=1, sticky=E)

self.entryTitle2 = Label(self.myFunctionContainer, text="Enter Markup $:")
self.entryTitle2.grid(padx=2, pady=2, row=3, column=1, sticky=E)

self.button = Button(self.myFunctionContainer, text="Calculate")
self.button.bind("<Button-1>", self.gpCalc1)
self.button.grid(padx=2, pady=2, row=4, column=1, columnspan=2, sticky=W+E)




def markup1(self, event):
a = [5,10,15,17,18,19,20,21,22,23,24,25,26,27,28,29,30,32,35,40,45,50,55]
colors = ['#DCDCDC','#FFFFFF','#DCDCDC','#FFFFFF','#DCDCDC','#FFFFFF','#DCDCDC','#FFFFFF',
'#DCDCDC','#FFFFFF','#DCDCDC','#FFFFFF','#DCDCDC','#FFFFFF','#DCDCDC','#FFFFFF',
'#DCDCDC','#FFFFFF','#DCDCDC','#FFFFFF','#DCDCDC','#FFFFFF','#DCDCDC','#FFFFFF']
b = 1
row = 4

while b < len(a):
gp = 1-((a[b-1]/100.0))
color = colors[b-1]
row = row + 1
markup = round((self.COG1.get()/gp),2)

self.gp1 = Label(self.myFunctionContainer, bg=color, width=6, relief=RIDGE, text=(((1-gp)*100),"%"))
self.gp1.grid(padx=2, pady=1, row=row ,column=1)

self.price1 = Label(self.myFunctionContainer, bg=color, relief=RIDGE, width=10, text=("$",markup))
self.price1.grid(padx=2, pady=1, row=row ,column=2)
b = b + 1

def markup2(self, event):
GP = 1 - (round((self.GP.get()/100),2))

self.price1 = Label(self.myFunctionContainer, relief=GROOVE, bg="white", width=10, font="bold", text=("$",round((self.COG2.get()/GP),2)))
self.price1.grid(pady=1, row=5 ,column=1, columnspan=2)

def increase1(self, event):
increase = 1+(self.increase.get()/100)

self.price1 = Label(self.myFunctionContainer, relief=GROOVE, bg="white", width=10, font="bold", text=("$",round((self.COG3.get()*increase),2)))
self.price1.grid(pady=1, row=5 ,column=1, columnspan=2)

def gpCalc1(self, event):
COG4 = self.COG4.get()
markup = self.markup.get()
if COG4==0.0 or markup==0.0:
self.price1 = Label(self.myFunctionContainer, relief=GROOVE, bg="white", width=10, font="bold", text="")
self.price1.grid(pady=1, row=5 ,column=1, columnspan=2)
else:
GP = 100*(1-(self.COG4.get()/self.markup.get()))

self.price1 = Label(self.myFunctionContainer, relief=GROOVE, bg="white", width=10, font="bold", text=(round(GP,2),"%"))
self.price1.grid(pady=1, row=5 ,column=1, columnspan=2)


def clearScreen(self):
self.clear = Label(self.myFunctionContainer)
self.clear.grid(row=1, column=0, rowspan=30, columnspan=6, sticky=N+E+S+W)
return

root = Tk()
myapp = MyApp(root)
root.mainloop()
 
C

cnb

you could prob abstract away a lot of that code, very similar-looking.

then it would be easier to find bugs.
 
P

Peter Otten

Kevin said:
# I posted a few days ago about a memory leak that I think i'm having
# with my first Tkinter program. I've had trouble pinpointing what is
# wrong so i thought i would submit the code and see if anyone would like
# to critique it.

Don't create new widgets every time you switch the tab. Instead hide/show
the relevant page. You may be able to reuse the idlelib.tabpage.TabPageSet
class for that.
# I have one more problem that i can't figure out either. There are a
# bunch of Entry widgets that require number values.
# My problem is if someone inputs a letter or space the program will
# have error that makes part of the Program nonfunctional.

You can wrap access to the DoubleVars with try...except, e. g.:

try:
value = self.COG1.get()
except ValueError:
# show an error message
else:
# continue calculation

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top