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
#5
Quote: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.
Code is useful for a lot of things. It is a very accurate description of the problem. It provides insight into you level of knowledge. It establishes a starting point for future conversations. It is best if you can post code that others can run to see the problem.
import tkinter as tk

def check(*args):
    print(float(txt1.get()))

root = tk.Tk()
txt1 = tk.StringVar(root, "0")
txt1.trace_add("write", check)
entr1 = tk.Entry(root, width=10, textvariable=txt1)
entr1.pack(padx=50, pady=50)
root.mainloop()
And then add instructions for how to make the error occur.
Quote:To make the error happen, select the text in the entry and type in a digit. You should get an error like this:
Error:
File "..test.py", line 4, in check print(float(txt1.get())) ValueError: could not convert string to float: ''

To answer your question, you get "" because pasting (or typing) in an entry deletes any selected text before entering new text. When there is a selection, the entry gets written twice. You'll see this if you run your example and alternate between typing with or without selecting text in the entry.
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] entry widget DPaul 5 1,736 Jul-28-2023, 02:31 PM
Last Post: deanhystad
  Get string from entry and use it in another script amdi40 1 1,506 Jan-26-2022, 07:33 PM
Last Post: Larz60+
  Tkinter Exit Code based on Entry Widget Nu2Python 6 3,213 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  method to add entries in multi columns entry frames in self widget sudeshna24 2 2,390 Feb-19-2021, 05:24 PM
Last Post: BashBedlam
  Entry Widget issue PA3040 16 7,186 Jan-20-2021, 02:21 PM
Last Post: pitterbrayn
  [Tkinter] password with Entry widget TAREKYANGUI 9 6,344 Sep-24-2020, 05:27 PM
Last Post: TAREKYANGUI
  [Tkinter] Get the last entry in my text widget Pedroski55 3 6,609 Jul-13-2020, 10:34 PM
Last Post: Pedroski55
  How to retreive the grid location of an Entry widget kenwatts275 7 4,889 Apr-24-2020, 11:39 PM
Last Post: Larz60+
  POPUP on widget Entry taratata2020 4 3,917 Mar-10-2020, 05:04 PM
Last Post: taratata2020
  [Tkinter] manipulation of string in Text widget Stauricus 2 3,173 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