微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用数字输入值进行计算

如何解决使用数字输入值进行计算

我正在用 tkinter 编写一个 GUI,我尝试使用一个入口小部件。 用户应在输入框中输入数值。在代码中,我编写了一个函数,它在计算中使用输入的数值,使用 .get() 方法接收输入的数值。 在函数的最后,计算出的值通过 .set() 返回到 GUI 中的输出字段。

代码给出了一个错误,我认为这与用户应该输入一个数值有关。 有谁知道如何使用输入小部件并明确应该在计算函数中输入和使用数值吗?

下面我添加了部分代码。我没有显示我的 excel 文件的路径,但在我自己的代码中,我确实输入了引用 excel 文件的路径。该文件由 3 个工作表组成,工作表名称用于下拉菜单中。当用户选择工作表名称时,以该工作表的数据为基础进行计算。

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,padx=2)               

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

        txt_residue_weight = tk.Entry(self,width=18)         
        txt_residue_weight['textvariable']= self.entry_weight  
        txt_residue_weight.grid(row=3,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,padx=2)  

        lbl_output= tk.Label(self,text='Result:')     
        lbl_output.grid(row=4,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,sticky= tk.W +tk.E,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)) 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。