Mar-05-2024, 07:52 PM
(This post was last modified: Mar-05-2024, 09:09 PM by deanhystad.)
Sorry i didnt include a sample as I didnt think it would help as you cant see the problem as such, as it happens when you select the contents of entry and type a new value. The code I have used I have replicated from others and videos as I am just starting out with python.
I have included a simple example of what I was doing that replicates the issue.
if I run this program and focus is set on the entry field and then type a number, in this example I pressed the number '6' the stringvar is updated twice, first with '' and i get the ValueError: could not convert string to float: '' and then as I would expect it is updated with '6'.
My program would only ever need positive numbers entered but the way I was doing it I now see was wrong
I have now updated my code now to reflect the way you have shown with a class and try/except and it works thank you.
However I am still interested to know why '' is being passed when I type '6' if youre able to shed any light on this?
Thanks for your help.
This is the sample code
I have included a simple example of what I was doing that replicates the issue.
if I run this program and focus is set on the entry field and then type a number, in this example I pressed the number '6' the stringvar is updated twice, first with '' and i get the ValueError: could not convert string to float: '' and then as I would expect it is updated with '6'.
My program would only ever need positive numbers entered but the way I was doing it I now see was wrong
I have now updated my code now to reflect the way you have shown with a class and try/except and it works thank you.
However I am still interested to know why '' is being passed when I type '6' if youre able to shed any light on this?
Thanks for your help.
This is the sample code
import tkinter as tk root = tk.Tk() txt1 = tk.StringVar() txt1.set("0") def check(*args): print(f'*{type(txt1.get())} - {txt1.get()}*') entr1 = tk.Entry(root, textvariable=txt1) entr1.place(x=20,y=20) txt1.trace_add("write", check) root.mainloop()
Output:*<class 'str'> - *
*<class 'str'> - 6*
deanhystad write Mar-05-2024, 09:09 PM:
Please post all code, output and errors (in it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Please post all code, output and errors (in it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.