2

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)) 
TheLizzard
  • 7,248
  • 2
  • 11
  • 31
marlen
  • 21
  • 1
  • You can have a look at this post https://stackoverflow.com/questions/4140437/interactively-validating-entry-widget-content-in-tkinter to see how use entry validation – j_4321 May 03 '21 at 09:07
  • I think you should also look at https://stackoverflow.com/questions/8959815/restricting-the-value-in-tkinter-entry-widget, it shows a way to restrict entry to only numeric vals. – typedecker May 03 '21 at 12:28
  • Also are you sure that the residue weight variable datatype should be string?, I think if it is involved in calculations it must be an integer. – typedecker May 03 '21 at 12:31
  • @MatrixProgrammer I think I made a mistake there. I ment indeed integer or float, but when I wrote int() or float(), I received an error... – marlen May 03 '21 at 14:10
  • @marlen can you maybe provide the traceback or the error, so we can better understand the problem. – typedecker May 03 '21 at 14:16
  • I seems like the error refers to the data in a specific sheet of my file since I recognize the values @MatrixProgrammer ```KeyError: "None of [Float64Index([1578803065.414701, 1578803065.414701, 1578803065.414701,\n ...\n dtype='float64')] are in the [columns]" ``` – marlen May 04 '21 at 11:58
  • Also, when I run the GUI, it immediately shows results of the most upper option in the optionmenu. The numeric values which can be found in the error above also relate to the most upper option of the option menu. (The chosen option in the option menu relates to a sheet name of which data is used). – marlen May 04 '21 at 12:02
  • Can you also tell @marlen that which line exactly is the error raised, it must be mentioned in the error, that can help me identify the problem maybe. – typedecker May 04 '21 at 13:45
  • @MatrixProgrammer It says: line 105, 18, 90, 2806, 1552, and 1640. This is also kind of weird since I have only 106 lines in total. – marlen May 05 '21 at 14:08
  • Some of the lines might be from a module you might be using wherein due to some fault in writing the code by you or some other problem the error has been raised. Thats why you might be getting errors in lines greater than the total lines in your code – typedecker May 05 '21 at 14:14

0 Answers0