468,458 Members | 1,877 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,458 developers. It's quick & easy.

Memory Leek, critique me. Thanks!!!

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

Aug 29 '08 #1
2 908
cnb
you could prob abstract away a lot of that code, very similar-looking.

then it would be easier to find bugs.
Aug 29 '08 #2
Kevin McKinley wrote:
# 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
Aug 29 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

37 posts views Thread by Eric | last post: by
11 posts views Thread by jong | last post: by
188 posts views Thread by christopher diggins | last post: by
5 posts views Thread by Sharon | last post: by
7 posts views Thread by Brano | last post: by
6 posts views Thread by madhu | last post: by
3 posts views Thread by crazy420fingers | last post: by
reply views Thread by Kevin McKinley | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.