0

was trying to implement a feature in a project i was doing where you enter text into an entry box and then it would times the amount of charaters in the entry by 0.02. i wanted to make it so there is a label and it would update automaticly as the user typed in the entry box but i cant seem to get it working

window = Tk()
window.geometry("600x500")
message_label = Label(window, text= "enter message").pack()
message_entry = Entry(window)
message_entry.pack()
message_length = (len(message_entry.get()))
message_price = message_length * 0.02


msg_price = Label(window)
msg_price.pack()
msg_price.config(text=message_price)

(i know this could be done easily with a button but im not trying to do this with a button)

  • Does this answer your question? [Tkinter ValueError: could not convert string to float: ''](https://stackoverflow.com/questions/70583530/tkinter-valueerror-could-not-convert-string-to-float) – Delrius Euphoria Jul 26 '22 at 14:02
  • Your label only gets updated once -- when this code runs. To update the label whenever your Entry is modified, you need to listen for that event, and then update the label every time a modification occurs. Alternatively, include a button that will call code that does this update when clicked. – Pranav Hosangadi Jul 26 '22 at 14:02
  • Does this answer your question? [How do I get an event callback when a Tkinter Entry widget is modified?](https://stackoverflow.com/questions/6548837/how-do-i-get-an-event-callback-when-a-tkinter-entry-widget-is-modified) – Pranav Hosangadi Jul 26 '22 at 14:03

3 Answers3

0

You can add a StringVar to an Entry and then use the trace function to get updates when it has been written to using the "w" mode.

myvar = StringVar()
myEntry = Entry(master, textvariable=myvar)
myvar.trace("w", lambda a, b, c, e=Event(), v=myvar, en=entry: get_output(a, b, c, e, v, en))
myEntry.pack(side=TOP)

def get_output(a, b, c, e, v, en): #a, b and c are autogenerated name, index and mode
   #Event() might not be needed but best to include it as it can sometimes be automatically passed
    print(en.get(0, END))
    print(sv.get())
Scott Paterson
  • 392
  • 2
  • 17
0

I would propose binding the KeyRelease event for the entry widget to a function. This, in my opinion, is simpler than using trace on a StringVar.

from tkinter import *

def updateLabel(e):
    message_length = (len(message_entry.get()))
    message_price = message_length * 0.02
    msg_price.config(text=message_price)

window = Tk()
window.geometry("600x500")
message_label = Label(window, text= "enter message")
message_label.pack()

message_entry = Entry(window)
message_entry.pack()
message_entry.bind('<KeyRelease>',updateLabel)

msg_price = Label(window)
msg_price.pack()


window.mainloop()

The updateLabel function will be updated after each key is released inside the message_entry widget

scotty3785
  • 6,763
  • 1
  • 25
  • 35
0

You can use this code (entry is named "e" and label is named "l")

var = StringVar()
e = Entry(root, textvariable=var)
l = Label(root, textvariable=var)
szachy-a
  • 21
  • 5