'How to avoid TclError if I want to update the results obtained from entries instantly?
I want to add two numbers and add them and update the result instantly without using a button.
I have written this code using after()
to do that.
from tkinter import *
root = Tk()
root.geometry("400x400")
label1 = Label(root, text="1st number:")
label1.grid(row=0, column=0)
label2 = Label(root, text="2nd number:")
label2.grid(row=1, column=0)
first_no = IntVar()
second_no = IntVar()
entry1 = Entry(root, textvariable=first_no)
entry1.grid(row=0, column=1)
entry2 = Entry(root, textvariable=second_no)
entry2.grid(row=1, column=1)
def auto():
try:
S = first_no.get() + second_no.get()
label3.config(text="Sum: " + str(S))
# call again after 100 ms
root.after(100, auto)
except TclError:
print("there is an Error")
label3 = Label(root)
label3.grid(row=3, column=1)
auto()
root.mainloop()
The code works well. However, if we delete the numbers in the entries using the delete
or backspace
keyboards this error raises: _tkinter.TclError: expected floating-point number but got ""
, and if I enter new values the code does not work anymore.
I wondered to know how I avoid this problem??
Solution 1:[1]
I found the solution. I added another root.after(100, auto)
under exception TclError
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |