Calculating with a numeric entry value

56 Views Asked by At

I am writing a GUI with tkinter, and I try to use an entry widget. Users should enter a numeric value within the entry box. Within the code I wrote a function which uses the entered numeric value in a calculation, using the .get() method to receive the entered numeric value. In the end of the function the calculated value is returned to an output field in the GUI with .set().

The code gives an error and I think it has to do with the fact that users should enter a numeric value. Does anyone know how to make use of the entry widget and make clear a numeric value should be entered and used within a calculation function?

Below I added parts of my code. I did not show the path of my excel file, but in my own code I did enter the path which refers to an excel file. This file consists out of 3 sheets, the sheetnames are used in the dropdown menu. When a sheetname is chosen by the user, the calculation is based on data of this sheet.

class user_framework(tk.Frame):     
    def __init__ (self):            
        tk.Frame.__init__(self)     
        self.master.title('Standardized ex-ante Framework')    
        self.optionsmenu = tk.StringVar()  
        self.existingmenu= tk.StringVar()  
        self.entry_weight= tk.StringVar()   
        self.processing_output= tk.StringVar()   
        self.entry_new_residue= tk.StringVar()  
        self.define_widgets()       
        self.residue_calculation()  
        self.grid()       

    def define_widgets(self):       
        lbl_residue_name= tk.Label(self, text='Residue name:') 
        lbl_residue_name.grid(row=2, column=0, sticky=tk.W, pady=2, padx=2)  

        xls = pd.read_excel(r"path",None) 
        residue_options = list(xls.keys())         
        self.optionsmenu.set(residue_options[0])    
        self.om = tk.OptionMenu(self, self.optionsmenu, *residue_options)  
        self.om.grid(row=2, column=1, sticky=tk.W, pady=2, padx=2)               

        lbl_residue_weight= tk.Label(self, text='Residue weight (kg):')  
        lbl_residue_weight.grid(row=3, column=0, sticky=tk.W, pady=2, padx=2)                    

        txt_residue_weight = tk.Entry(self, width=18)         
        txt_residue_weight['textvariable']= self.entry_weight  
        txt_residue_weight.grid(row=3, column=1, sticky=tk.W, pady=2, padx=2)   

        btn_calculate = tk.Button(self, text='Solve', background='green', relief='groove', width=7 ,borderwidth=5, cursor='spider')  
        btn_calculate['command'] = lambda:[self.residue_calculation()]  
        btn_calculate.grid(row=2, column=2, columnspan=2, sticky=tk.W, pady=2, padx=2)  

        lbl_output= tk.Label(self, text='Result:')     
        lbl_output.grid(row=4, column=0, sticky=tk.W, pady=2, padx=2)    

        value_output= tk.Label(self, anchor=tk.CENTER, relief='ridge', background='white')  
        value_output['textvariable']= self.processing_output                      
        value_output.grid(row=4, column=1, columnspan=2, sticky= tk.W +tk.E, pady=2, padx=2)

    def residue_calculation(self):  
        residue_choice= self.optionsmenu.get()
        residue_choice= str(residue_choice) 
        residue_weight= self.entry_weight.get()
        residue_weight= str(residue_weight)
        residue_sheet = pd.read_excel(r"path", sheet_name=residue_choice) #path refers to an excel file, within my own code I specified the path, so it is linked to an excel sheet
        weight_column= residue_sheet[residue_sheet.CO2equivalent] #weight_column indicates the specified sheet and column with CO2 values which should be multiplied by the entered weight
        weight_calculation= weight_column * residue_weight
        self.processing_output.set(float(weight_calculation)) 
0

There are 0 best solutions below