我正在创建此程序来计算集中营中动物的体重,方法是获取每个年龄段的动物数量,然后获取其每天所需的食物总量。 我将Tkinter用于GUI,其中输入了每个年龄段的动物数量。 当我运行程序时,程序不会检索输入的值并且无法使用它们? 我对Tkinter完全陌生,对Python 3.6也不陌生 对于任何建设性的批评,我将深表感谢。
#Beginning
from tkinter import *
from math import *
import string
root = Tk()
#Function to calculate everything- Gets total weight of animals and then calculates feed
def enter_click(event):
global SabCow, SabBull, BuffBull, BuffCow, RoanCow, RoanBull, Six_Months,
One_Year, Two_Year, Three_Year, Big
M1_int = IntVar(M1.get())
M2_int = IntVar(M2.get())
M3_int = IntVar(M3.get())
M4_int = IntVar(M4.get())
M5_int = IntVar(M5.get())
F1_int = IntVar(F1.get())
F2_int = IntVar(F2.get())
F3_int = IntVar(F3.get())
F4_int = IntVar(F4.get())
F5_int = IntVar(F5.get())
#Work with which animal is selected form radiobutton, then calculate what is the weight of animals of each age then calculate food necessary
if A.get() == 1 :
f1_weight = F1_int * ((SabCow * 25)/Six_Months)
f2_weight = F2_int * ((SabCow * 40)/One_Year)
f3_weight = F3_int * ((SabCow * 70)/Two_Year)
f4_weight = F4_int * ((SabCow * 85)/Three_Year)
f5_weight = F5_int * SabCow
m1_weight = M1_int * ((SabBull * 25)/Six_Months)
m2_weight = M2_int * ((SabBull * 40)/One_Year)
m3_weight = M3_int * ((SabBull * 70)/Two_Year)
m4_weight = M4_int * ((SabBull * 85)/Three_Year)
m5_weight = M5_int * SabBull
Weight_Female = lambda f1_weight, f2_weight, f3_weight, f4_weight, f5_weight : f1_weight + f2_weight + f3_weight + f4_weight + f5_weight
Weight_Male = m1_weight + m2_weight + m3_weight + m4_weight + m5_weight
Total_Weight = Weight_Female + Weight_Male
Total_Food = Total_Weight * Big
animal = "Sable"
result_text = animal, "\nTotalFood Requierd: ", Total_Food, "\n", Total_Weight, "\n", Weight_Female, "\n", Weight_Male
return result_text
elif A.get() == 2 :
f1_weight = F1_int * ((BuffCow * 25)/Six_Months)
f2_weight = F2_int * ((BuffCow * 40)/One_Year)
f3_weight = F3_int * ((BuffCow * 70)/Two_Year)
f4_weight = F4_int * ((BuffCow * 85)/Three_Year)
f5_weight = F5_int * SabCow
m1_weight = M1_int * ((BuffBull * 25)/Six_Months)
m2_weight = M2_int * ((BuffBull * 40)/One_Year)
m3_weight = M3_int * ((BuffBull * 70)/Two_Year)
m4_weight = M4_int * ((BuffBull * 85)/Three_Year)
m5_weight = M5_int * BuffBull
Weight_Female = f1_weight + f2_weight + f3_weight + f4_weight + f5_weight
Weight_Male = m1_weight + m2_weight + m3_weight + m4_weight + m5_weight
Total_Weight = Weight_Female + Weight_Male
Total_Food = Total_Weight * Big
animal = "Buffalo"
result_text = animal, "\nTotalFood Requierd: ", Total_Food, "\n", Total_Weight, "\n", Weight_Female, "\n", Weight_Male
return result_text
elif A.get() == 3 :
f1_weight = F1_int * ((RoanCow * 25)/Six_Months)
f2_weight = F2_int * ((RoanCow * 40)/One_Year)
f3_weight = F3_int * ((RoanCow * 70)/Two_Year)
f4_weight = F4_int * ((RoanCow * 85)/Three_Year)
f5_weight = F5_int * RoanCow
m1_weight = M1_int * ((RoanBull * 25)/Six_Months)
m2_weight = M2_int * ((RoanBull * 40)/One_Year)
m3_weight = M3_int * ((RoanBull * 70)/Two_Year)
m4_weight = M4_int * ((RoanBull * 85)/Three_Year)
m5_weight = M5_int * RoanBull
Weight_Female = f1_weight + f2_weight + f3_weight + f4_weight + f5_weight
Weight_Male = m1_weight + m2_weight + m3_weight + m4_weight + m5_weight
Total_Weight = Weight_Female + Weight_Male
Total_Food = Total_Weight * Big
animal = "Roan"
result_text = animal, "\nTotalFood Requierd: ", Total_Food, "\n", Total_Weight, "\n", Weight_Female, "\n", Weight_Male
return result_text
print(result_text)
animal = StringVar()
#Animal Weight
BuffBull = 750
BuffCow = 650
SabBull = 230
SabCow = 210
RoanBull = 270
RoanCow = 240
#Animal Ages to Weight
Six_Months = 125
One_Year = 140
Two_Year = 170
Three_Year = 185
#Percentage Food needed of total KG
Big = 2/102
Small = 5/105
#Tkinter
A = IntVar()
A.set(1)
result_text = StringVar()
f1_weight = DoubleVar()
f2_weight = DoubleVar()
f3_weight = DoubleVar()
f4_weight = DoubleVar()
f5_weight = DoubleVar()
m1_weight = DoubleVar()
m2_weight = DoubleVar()
m3_weight = DoubleVar()
m4_weight = DoubleVar()
m5_weight = DoubleVar()
#GUI
w =Label(root, text="Choose an Animal:", justify=LEFT, padx=5,pady=10).grid(row=0)
o =Label(root, text="Results:", justify=LEFT, padx=5, pady=10).grid(row=7)
Label(root, text="Age", padx=5, pady=20).grid(row=0, column=2)
Label(root, text="M", padx=5, pady=20).grid(row=0, column=3)
Label(root, text="F", padx=5, pady=20).grid(row=0, column=4)
Radiobutton(root, text="Sable", padx=20, variable=A, value=1).grid(row=1)
Radiobutton(root, text="Buffalo", padx=20, variable=A, value=2).grid(row=2)
Radiobutton(root, text="Roan", padx=20, variable=A, value=3).grid(row=3)
Label(root, text="6 Months :").grid(row=1, column=2)
Label(root, text="1 Year :").grid(row=2, column=2)
Label(root, text="2 Years :").grid(row=3, column=2)
Label(root, text="3 Years :").grid(row=4, column=2)
Label(root, text="4 Years :").grid(row=5, column=2)
#Entry widgets-to get total of animals of each age
M1 = Entry(root)
M2 = Entry(root)
M3 = Entry(root)
M4 = Entry(root)
M5 = Entry(root)
F1 = Entry(root)
F2 = Entry(root)
F3 = Entry(root)
F4 = Entry(root)
F5 = Entry(root)
M1.grid(row=1, column=3)
M2.grid(row=2, column=3)
M3.grid(row=3, column=3)
M4.grid(row=4, column=3)
M5.grid(row=5, column=3)
F1.grid(row=1, column=4)
F2.grid(row=2, column=4)
F3.grid(row=3, column=4)
F4.grid(row=4, column=4)
F5.grid(row=5, column=4)
#Calculation button and event
enter_button=Button(root, text="Enter")
enter_button.grid(row=6)
enter_button.bind("<Enter>",enter_click)
enter_button.bind("<Return>",enter_click)
root.mainloop()
在此阶段,我在没有帮助的情况下对互联网进行了2周的搜索,现在在运行该程序时也遇到了以下错误: AttributeError:“ str”对象没有属性“ _root”
答案 0 :(得分:2)
您的代码中存在多个问题。
第一:
M1_int = IntVar(M1.get())
应该是:
M1_int = float(M1.get())
(适用于M1 ... F7中的所有变量)
第二:在Weight_Female
计算中,您不需要lambda。
第三:A == 3
始终为False
。您需要像第一种情况(A.get() == 1
)一样获得价值形式控制。