0

Been stuck on this one for a while now please help as im bit of a noob

none the less here is the code

from tkinter import*
###title/info 
root = Tk()
root.configure(background="black")
root.title("C02 caculator")
root.resizable(1,1)
root.columnconfigure(0,weight=1)
root.columnconfigure(1,weight=1)

introtext = Label(root, text=" C02 Calculator \n" 
'Made by: Ronan Shade \n'
'Date: 18/7/2018 \n '

"\n This calculator is made to find out the O/A c02 output from any given product, \n"
"using a list of modified and combined formulas . Simply weigh (in grams) the \n" 
"wight of each core part of the product and determine what marital the part \n" 
"made of. Enter the findings into the calculator and it will give you a output.\n"

"\n Data used to make the formula can be found at the following: \n"
"http://www.lowtechmagazine.com/what-is-the-embodied-energy-of-materials.html \n"
"https://www.unitjuggler.com/convert-energy-from-kgSKE-to-MJ.html \n"
"https://carbonpositivelife.com/co2-per-kwh-of-electricity/ \n"
"http://web.mit.edu/ebm/www/publications.htm \n"
,background="black",foreground="grey90")
introtext.grid(row=1,column=0, sticky=E)



root.mainloop()

### here is where the real work starts ###
def final_calculation():
    entryField.delete(0,'end') 

    Steal = float(entrySteal.get())
    Steal_recycled= float(entryRecycledSteal.get())
    Wood = float(entryWood.get())
    Aluminum_recycled = float(entryAluminumRrecycled.get())
    Aluminum = float(entryAluminum.get())
    Iorn = float(entryIron.get())
    Glass = float(entryGlass.get())
    Paper = float(entryPaper.get())
    Plastics = float(entryPlastics.get())
    Copper = float(entryCopper.get())
    Silicon = float(entrySilicon.get())
    Nickel = float(entryNickel.get())
    Titanium = float(entryTitanium.get())
    Electronic_grade_silicon= float(entryElectronicGradeSilicon.get())

    output_wood = (Wood *wood_c02)
    output_steal = (Steal * steal_c02 )
    output_Steal_recycled = (Steal_recycled*Steal_recycled_c02)
    output_Aluminum = (Aluminum*Aluminum_c02)
    output_Aluminum_recycled = (Aluminum_recycled*Aluminum_recycled_c02)
    output_Iorn = (Iorn*Iorn_c02)
    output_Glass = (Glass*Glass_c02)
    output_Paper = (Paper*Paper_c02)
    output_Plastics = (Plastics*Plastics_c02)
    output_Copper = (Copper*Copper_c02)
    output_Silicon = (Silicon*Silicon_c02)
    output_Nickel = (Nickel*Nickel_c02)
    output_Titanium = (Titanium*Titanium_c02)
    output_Electronic_grade_silicon = (Electronic_grade_silicon*Electronic_grade_silicon_c02)

    FinalMess = (output_steal + output_wood + output_Steal_recycled + output_Aluminum + output_Aluminum_recycled
    + output_Aluminum +output_Iorn + output_Glass +output_Paper + output_Plastics + output_Copper + output_Silicon
    + output_Nickel + output_Titanium + output_Electronic_grade_silicon * c02_coal)

    FinalClean="the amount of C02 in Grams = " + str (round(FinalMess,2))
    entry.set(FinalClean)

steal_c02 = float (9.73)
Steal_recycled_c02 = float (2.91)
wood_c02 = float (1.39)
Aluminum_recycled_c02 = float (3.95)
Aluminum_c02 = float (60.8)
Iorn_c02 = float (3.75)
Glass_c02 = float (7.35)
Paper_c02 = float (10.43)
Plastics_c02 = float (15.57)
Copper_c02 = float (25.65)
Silicon_c02 = float (64.6)
Nickel_c02 = float (69.45)
Titanium_c02 = float (255.5)
Electronic_grade_silicon_c02 = float (2131.8)
c02_coal =+ float (0.94) # number used to caculate kwh to C02





root = Tk()
root.configure(background="black")
root.title("C02 caculator")
root.resizable(1,1)
root.columnconfigure(0,weight=1)
root.columnconfigure(1,weight=1)

entry = StringVar()
entryField = Entry(root, textvariable=entry,background="white",foreground="black",justify=CENTER)
entryField.grid(row=0,column=0,columnspan=2,sticky=W+E)
entryField.columnconfigure(0,weight=1)

#lable for all the products 
labelWood = Label(root, text="Amount of wood :",background="black",foreground="grey90")
labelWood.grid(row=1,column=0, sticky=E)

labelSteal = Label(root, text="Amount of steal :",background="black",foreground="grey90")
labelSteal.grid(row=2,column=0, sticky=E)

labelRecycledSteal = Label(root, text="Amount of recycled steal :",background="black",foreground="grey90")
labelRecycledSteal.grid(row=3,column=0, sticky=E)

labelAluminum = Label(root, text="Amount of aluminum :",background="black",foreground="grey90")
labelAluminum.grid(row=4,column=0, sticky=E)

labelAluminumRrecycled = Label(root, text="Amount of aluminum rrecycled :",background="black",foreground="grey90")
labelAluminumRrecycled.grid(row=5,column=0, sticky=E)

labelIron = Label(root, text="Amount of iron :",background="black",foreground="grey90")
labelIron.grid(row=6,column=0, sticky=E)

labelGlass = Label(root, text="Amount of glass :",background="black",foreground="grey90")
labelGlass.grid(row=7,column=0, sticky=E)

labelPaper = Label(root, text="Amount of paper :",background="black",foreground="grey90")
labelPaper.grid(row=8,column=0, sticky=E)

labelPlastics = Label(root, text="Amount of plastics :",background="black",foreground="grey90")
labelPlastics.grid(row=9,column=0, sticky=E)

labelCopper = Label(root, text="Amount of copper :",background="black",foreground="grey90")
labelCopper.grid(row=10,column=0, sticky=E)

labelSilicon = Label(root, text="Amount of Silicon :",background="black",foreground="grey90")
labelSilicon.grid(row=11,column=0, sticky=E)

labelNickel = Label(root, text="Amount of nickel :",background="black",foreground="grey90")
labelNickel.grid(row=12,column=0, sticky=E)

labelTitanium = Label(root, text="Amount of titanium :",background="black",foreground="grey90")
labelTitanium.grid(row=13,column=0, sticky=E)

labelElectronicGradeSilicon = Label(root, text="Amount of electronic grade silicon : ",background="black",foreground="grey90")
labelElectronicGradeSilicon.grid(row=14,column=0, sticky=E)



#entry boxes 
entryWood = Entry (root,background="grey90",foreground="black") 
entryWood.grid(row=1,column=1,sticky=W)


entrySteal = Entry(root,background="grey90",foreground="black")
entrySteal.grid(row=2,column=1,sticky=W)


entryRecycledSteal = Entry(root,background="grey90",foreground="black")
entryRecycledSteal.grid(row=3,column=1,sticky=W)

entryAluminum = Entry(root,background="grey90",foreground="black")
entryAluminum.grid(row=4,column=1,sticky=W)

entryAluminumRrecycled = Entry(root,background="grey90",foreground="black")
entryAluminumRrecycled.grid(row=5,column=1,sticky=W)

entryIron = Entry(root,background="grey90",foreground="black")
entryIron.grid(row=6,column=1,sticky=W)

entryGlass = Entry(root,background="grey90",foreground="black")
entryGlass.grid(row=7,column=1,sticky=W)

entryPaper = Entry(root,background="grey90",foreground="black")
entryPaper.grid(row=8,column=1,sticky=W)

entryPlastics = Entry(root,background="grey90",foreground="black")
entryPlastics.grid(row=9,column=1,sticky=W)

entryCopper = Entry(root,background="grey90",foreground="black")
entryCopper.grid(row=10,column=1,sticky=W)

entrySilicon = Entry(root,background="grey90",foreground="black")
entrySilicon.grid(row=11,column=1,sticky=W)

entryNickel = Entry(root,background="grey90",foreground="black")
entryNickel.grid(row=12,column=1,sticky=W)

entryTitanium = Entry(root,background="grey90",foreground="black")
entryTitanium.grid(row=13,column=1,sticky=W)

entryElectronicGradeSilicon = Entry(root,background="grey90",foreground="black")
entryElectronicGradeSilicon.grid(row=14,column=1,sticky=W)

#caculate button
button = Button(root, text="caculate C02", command= final_calculation)
button.grid(row=15,columnspan=2)




root.mainloop()

as you can probably see my issue a hell of a lot of input options when really the user will only be needing a couple of them, but its a massive pain to manually input 0's into every input that is not being used. is there any way to set the input to 0 by default

kind regards 49.95

the49.95
  • 25
  • 6

1 Answers1

1

It is a pain to do something for all your entries, because you create all of them manually. It would be easier to create them in a loop so you can do any modification, like inserting a 0, to all of them at the same time:

materials = ['wood', 'steal', 'recycled steal', 'aluminum', 'recycled aluminum', 'iron', 'glass', 'paper', 'plastics', 'copper', 'silicon', 'nickel', 'titanium', 'electronic grade silicon']
labels = {}
entries = {}

for i, m in enumerate(materials):
    labels[m] = Label(root, text="Amount of {} :".format(m), background="black", foreground="grey90")
    labels[m].grid(row=i+1,column=0, sticky=E)

    entries[m] = Entry(root, background="grey90", foreground="black") 
    entries[m].grid(row=i+1,column=1,sticky=W)
    entries[m].insert(END, '0')

Keep in mind that if you use this you need to rewrite your final_calculation function to get the values in the following way: Steal = float(entries['steal'].get())

Also, you might want to read up on entry validation to make sure that what's in the entry can be converted to a float.

fhdrsdg
  • 10,297
  • 2
  • 41
  • 62