pythontkintertkinter-label

Updating tkinter label value dynamically without using button


I know how to update the label with the command assigned to the button. but I want it to do this dynamically with entry inputs, without buttons.

enter image description here

i want to change label value dynamically while user input entries.

example function is;

def calculation():

    a = var2.get() \* var3.get()
    var1.set(a)

Solution

  • import tkinter as tk
    
    def calculation(event=None):
        try:
            if input_entry1.get() and input_entry2.get():
                a = float(input_entry1.get()) * float(input_entry2.get())
                output_label["text"]=a
        except ValueError:
            output_label["text"] = "Please enter numbers only"
    
    root=tk.Tk()
    input_entry1=tk.Entry(root)
    input_entry1.pack()
    input_entry2=tk.Entry(root)
    input_entry2.pack()
    output_label=tk.Label(root)
    output_label.pack()
    input_entry1.bind("<KeyRelease>", calculation)
    input_entry2.bind("<KeyRelease>", calculation)
    root.mainloop()
    

    Hope this helps.