首页 > 解决方案 > How to work with Values entered through Tkinter entry widgets

问题描述

I'm creating this program to calculate the weight of animals in a camp, by getting the amount of animals of each age and then get the total amount of food they need daily. I'm using Tkinter for the GUI where the amount of animals of each age is entered. When i run the program the program doesn't retrieve the values that where entered and doesn't work with them? I am completely new to Tkinter and fairly new to Python 3.6 I would be extremely grateful for any constructive criticism.

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

At this stage I've scouted the internet for 2 weeks with no help and now I also get this error when running the program: AttributeError: 'str' object has no attribute '_root'

标签: python-3.xtkinter

解决方案


There is a multiple problems in your code.

First:

M1_int = IntVar(M1.get())

Should be like:

M1_int = float(M1.get())

(it is for all variables in M1...F7)

Second: in Weight_Female calculation you doesn't need lambda.

Third:A == 3 is always False. You need to get value form control like in first case (A.get() == 1).


推荐阅读