How to detect text change of an entry in tkinter Python

Viewed 42

How can detect that a user entering characters in tkinter entry ? I want to calculate the total cost from 2 different entry. here is my code but does not work!

from tkinter import *

root=Tk()

def calculate_total_cost(event):
    if count_ent.get().isdigit() and unit_cost_ent.get().isdigit():
            total_cost=int(count_ent.get())*int(unit_cost_ent.get())
            print(total_cost)

count_ent=Entry(root).pack()
unit_cost_ent=Entry(root).pack()
unit_cost_ent.bind("<key>",calculate_total_cost)
1 Answers

Please check this, insert value in both entry and press enter, you will get the results. Although your questions is also not cleared, but from your statement I decided that you are facing issue of "AttributeError: 'NoneType' object has no attribute 'bind'"... By executing the below code, I hope you will get your answer. First execute this simple program, you will get the desired output in terminal.


from tkinter import *

root=Tk()

def calculate_total_cost(event):
    if count_ent.get().isdigit() and unit_cost_ent.get().isdigit():
         total_cost=int(count_ent.get())*int(unit_cost_ent.get())
         print(total_cost)


count_ent=Entry(root)
count_ent.pack()
# count_ent.insert(0, value1)
unit_cost_ent=Entry(root)
unit_cost_ent.pack()
# unit_cost_ent.insert(0, value2)

unit_cost_ent.bind("<Return>",calculate_total_cost)

root.mainloop()
Related