在没有输入的情况下将Python3输入设置为默认数字

时间:2018-07-24 08:56:14

标签: python input tkinter error-handling

暂时停留在此位置上已有一段时间,请给我一点帮助

这里的代码越少

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

您可能会看到我的问题,当真正的用户只需要几个输入选项时,就会遇到很多输入选项,但是手动将0输入到每个未使用的输入中会带来很大的麻烦。有什么方法可以将输入默认设置为0

亲切问候49.95

1 个答案:

答案 0 :(得分:1)

为所有条目执行某项操作很麻烦,因为您手动创建了所有条目。在循环中创建它们会更容易,因此您可以同时对所有它们进行任何修改,例如插入0:

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')

请记住,如果使用此方法,则需要重写final_calculation函数以通过以下方式获取值:Steal = float(entries['steal'].get())

此外,您可能希望阅读entry validation,以确保条目中的内容可以转换为浮点数。

相关问题