0

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)
Nelewout
  • 6,281
  • 3
  • 29
  • 39
Okthai
  • 3
  • 2
  • Can you please share some more code? – Abhishek Chaudhari Jul 08 '23 at 16:04
  • the question needs sufficient code for a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – D.L Jul 08 '23 at 17:21
  • 1
    why do you have a backslash in front of `*`? Is that a typo? – Bryan Oakley Jul 08 '23 at 17:26
  • Just have `calculation()` schedule itself to be called later (perhaps with a 100 to 500 millisecond delay), using `.after()`. Call it yourself once, to get the process started. Note that you will need a `try`/`except` to handle the likelihood that at least one of the values wasn't in a valid state at the moment the calculation was attempted. – jasonharper Jul 08 '23 at 18:57

1 Answers1

0
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.

kimhyunju
  • 309
  • 1
  • 7