0

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)
Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 20 '22 at 07:59
  • you can add `Button` to execute function. You can bind event `` to run code when you press `ENTER`/`RETURN`, You assign `StringVar` to `Entry` as `textvariable` and use `trace` to assign function to StringVar and it will execute this function when it changes text in `StringVar` . You can bind even `` to run function when you jump to another widget. And similar you can use `validatecommand=` – furas Sep 20 '22 at 10:25

1 Answers1

0

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()
  • your code is working , but I want to use an event that call the "calculate_total_cost" by entering every character in the entry. whats that event name? – Jalal Alipoor Sep 20 '22 at 10:05
  • @JalalAlipoor it is `` (or ``) with upper `K`. But it runs function before adding char - so it gets value without last char. Better can be `` which will execute function after adding char to `Entry`, – furas Sep 20 '22 at 10:28
  • Thanks a lot , the true event was keyrelease. Thank you – Jalal Alipoor Sep 20 '22 at 10:33