Search code examples
pythonuser-interfacebindtkinter-entrytkinter-label

How to update label value when an entry value has changed without using any button?


I'm creating a program where I have two entries for receiving some values from the user. With that values, I want to make some calculations and display the result in a label. I don´t want to use any kind of button or widgets to refresh the value of the label. I've tried using bind events for that, but I haven't found any event that works when the entry value changes. I hope that my question is understood. Thankyou.


    self.Capacity_Entry=Entry(self.Window,borderwidth=3,textvariable=self.Capacity)

    self.Capacity_Entry.grid(row=3,column=0)

    self.Capacity_Entry.bind('<Enter>',self.calculate_unknown_variable)

    self.Voltage_Entry=Entry(self.Window,borderwidth=3,textvariable=self.Voltage)

    self.Voltage_Entry.grid(row=3,column=2)

    self.Voltage_Entry.bind('<Enter>',self.calculate_unknown_variable)

    self.Energy_label=Label(self.Window)

    self.Energy_label.grid(row=1,column=4,)


    def calculate_unknown_variable(self,*args):

        self.V=float(self.Voltage.get())

        self.C=float(self.Capacity.get())

        self.E=0.5*self.C*self.V*self.V

        self.Result_Label.config(text=self.E)


Solution

  • You can use "<KeyRelease>" event to do so.

    Everytime you release a key on keyboard, on releasing the key the method associated will be triggered.

    self.Capacity_Entry.bind("<KeyRelease>",self.calculate_unknown_variable))