Python Forum
ValueError: could not convert string to float: '' fron Entry Widget
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ValueError: could not convert string to float: '' fron Entry Widget
#4
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

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.
Reply


Messages In This Thread
RE: ValueError: could not convert string to float: '' fron Entry Widget - by russellm44 - Mar-05-2024, 07:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] entry widget DPaul 5 1,764 Jul-28-2023, 02:31 PM
Last Post: deanhystad
  Get string from entry and use it in another script amdi40 1 1,512 Jan-26-2022, 07:33 PM
Last Post: Larz60+
  Tkinter Exit Code based on Entry Widget Nu2Python 6 3,232 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  method to add entries in multi columns entry frames in self widget sudeshna24 2 2,409 Feb-19-2021, 05:24 PM
Last Post: BashBedlam
  Entry Widget issue PA3040 16 7,231 Jan-20-2021, 02:21 PM
Last Post: pitterbrayn
  [Tkinter] password with Entry widget TAREKYANGUI 9 6,386 Sep-24-2020, 05:27 PM
Last Post: TAREKYANGUI
  [Tkinter] Get the last entry in my text widget Pedroski55 3 6,638 Jul-13-2020, 10:34 PM
Last Post: Pedroski55
  How to retreive the grid location of an Entry widget kenwatts275 7 4,914 Apr-24-2020, 11:39 PM
Last Post: Larz60+
  POPUP on widget Entry taratata2020 4 3,937 Mar-10-2020, 05:04 PM
Last Post: taratata2020
  [Tkinter] manipulation of string in Text widget Stauricus 2 3,189 Feb-17-2020, 09:23 PM
Last Post: Stauricus

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020