【问题标题】:Set Python3 input To a default number when no input is given未给出输入时将 Python3 输入设置为默认数字
【发布时间】:2018-12-31 19:57:31
【问题描述】:

被这个卡住了一段时间了,请帮我作为一个菜鸟

这里是代码

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

【问题讨论】:

标签: python input tkinter error-handling


【解决方案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 以确保条目中的内容可以转换为浮点数。

【讨论】:

  • 谢谢你我早上试一试,然后再告诉你我的进展情况
猜你喜欢
  • 1970-01-01
  • 2011-10-18
  • 2012-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-25
  • 1970-01-01
  • 2021-12-05
相关资源
最近更新 更多