472,976 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,976 software developers and data experts.

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 1055
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Eric | last post by:
There is a VB.NET critique on the following page: http://www.vb7-critique.741.com/ for those who are interested. Feel free to take a look and share your thoughts. Cheers, Eric. Ps: for those...
11
by: jong | last post by:
I have a problem with an C# application. The application is a 24x7 low volume message processing server. The server has a single thread of processing, running in a continuous loop, for each...
188
by: christopher diggins | last post by:
I have posted a C# critique at http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up the following issues : - unsafe code - attributes - garbage collection -...
5
by: Sharon | last post by:
Hi to all. My program starts at about 7000 KB memory consumption. After few hours it gets to over 200 MB. Running GC.Collect() periodically did not help, so it must be a leak. How can i find...
7
by: Brano | last post by:
Hi all, I have a VB.NET Dll that is invoked via BizTalk 2002 AIC over Http protocol. the Dll is making a connection using a 3rd party connector to a Unidata database (old legacy stuff) All I...
0
by: Brano | last post by:
HI all The problem is I have a 3rd party class that doesnt have a dispose method. It has however a OpenConnection method and a CloseConnection method but I call both of these and the class...
6
by: madhu | last post by:
vector<vector<vector<long Vector3D; // 3dvector. for (long k = 0; j < Depth; j++ ) { Vector3D.push_back ( vector<vector<A_Type() ); for (long j = 0; j < Height; j++ ) { Vector3D.push_back (...
3
by: crazy420fingers | last post by:
I'm running a python program that simulates a wireless network protocol for a certain number of "frames" (measure of time). I've observed the following: 1. The memory consumption of the program...
0
by: Kevin McKinley | last post by:
cnb, That is on of the things i want to fix with this code. I meant to mention in my original post that i've only been programming with Python for a little over two weeks. The only other...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.