-1

How can I take a value a user inputs into a tkinter Entry widget and store it as a float? I have created a tkinter Entry widget so the user can input a decimal value:

from tkinter import *
window = Tk()
a = Entry()
a.pack()
number = float(a.get())
window.mainloop()

I get an error from number = float(a.get()) that states: ValueError: could not convert string to float: ''. Is this because the tkinter Entry widget is empty without further configurations?

Edit: found solution: bind the tkinter Entry widget using tkinter.Entry.bind(). I set the parameters of bind() to <Return> followed by a comma and a lambda function.

  • 4
    Because the entry hand the value of "" (empty string) when the line `number = float(a.get())` was executed. – Igor Dragushhak Jun 21 '20 at 23:32
  • How would I execute `number = float(a.get())` only after the entry value is entered? – ephemeralhappiness Jun 21 '20 at 23:38
  • 1
    You would need to make an event callback function and trace it. https://stackoverflow.com/questions/6548837/how-do-i-get-an-event-callback-when-a-tkinter-entry-widget-is-modified – Igor Dragushhak Jun 21 '20 at 23:41
  • 2
    I think what @Igor initially said is correct. `tkinter` programs are event-driven, which means you should only call `a.get()` in a callback function that is invoked when certain events occur. One way is to `a.bind('', my_function)` which will call the function whenever the Enter key is pressed (when the `Entry` widget has focus). Even then you will need to make sure the value isn't the empty string to avoid the same error. – martineau Jun 21 '20 at 23:42
  • Or bind it https://stackoverflow.com/questions/39058817/event-callback-after-a-tkinter-entry-widget?rq=1 – Igor Dragushhak Jun 21 '20 at 23:44
  • 1
    @martineau Thanks, the binding method works perfectly for what I need. – ephemeralhappiness Jun 21 '20 at 23:48
  • All your script display and bindings should be in the main body of your script. Everything that should be performed after the window is displayed should go in the callback functions of the bindings. – Igor Dragushhak Jun 21 '20 at 23:48
  • @IgorDragushhak Makes sense now. Thank you for a simple-to-follow explanation! – ephemeralhappiness Jun 21 '20 at 23:49
  • 1
    ephemeralhappiness: That's good to hear. Sounds like you might be interested in [Best way to structure a tkinter application?](https://stackoverflow.com/questions/17466561/best-way-to-structure-a-tkinter-application) – martineau Jun 21 '20 at 23:56
  • Haha what are the odds...I reference that question for the GUI I'm working on for a research project. I'm a high school student fairly new to Python, but I'm learning a helluva lot because of people like you on stack overflow. Thank you! – ephemeralhappiness Jun 22 '20 at 00:09

1 Answers1

-1

You may find this helpful.

from tkinter import *
try:

    window = Tk()
    some = StringVar()
    a = Entry(textvariable = some)
    a.pack()
    window.mainloop()

    number = float(some.get())
    print(number)
except ValueError:
    print("Invalid Value in The Entry Box")
Ayush Jain
  • 154
  • 1
  • 7
  • This will give the error too. By default value of `Entry` Widget is `""` and `StringVar` will not do anything here. **This is not the answer.** – Saad Jun 22 '20 at 07:00
  • **Maybe with a proper `Trace` callback function to `DoubleVar`** with `if` condition should be more advisable. – Saad Jun 22 '20 at 07:05
  • DoubleVar Will also give a similar error when a string is received. – Ayush Jain Jun 22 '20 at 07:09