0

I'm writing a simple math program, and it's my first UI based app. I'm trying to get entry and turn it into a number:

s2E=tk.Entry()
s3E=tk.Entry()
s1Str=str(s1E)
s2Str=str(s2E)
s3Str=str(s3E)
s1=int(s1Str)
s2=int(s2Str)
s3=int(s3Str)

However, it returns an error:

File "C:\Users\Owner\Desktop\Programming\PythonPrograms\UItest.py", line 19, in <module>                                                                                                                                         s1=int(s1Str)
ValueError: invalid literal for int() with base 10: '.!entry'
Syscall
  • 19,327
  • 10
  • 37
  • 52
  • 1
    Does this answer your question? [tkinter, how to get the value of an Entry widget?](https://stackoverflow.com/questions/47378715/tkinter-how-to-get-the-value-of-an-entry-widget) – Torxed May 25 '20 at 19:26
  • No, as I do this I still get "ValueError: invalid literal for int() with base 10: ''" – candyDoesPrograms May 25 '20 at 19:30
  • From what I can tell, the error is that it's trying to convert the entry, which doesn't exist yet? – candyDoesPrograms May 25 '20 at 19:30
  • I think you have the same problem like in [post](https://stackoverflow.com/questions/47714032/valueerror-invalid-literal-for-int-with-base-10-on-tkinter-entry-and-pos) please check [this](https://stackoverflow.com/questions/47714032/valueerror-invalid-literal-for-int-with-base-10-on-tkinter-entry-and-pos) answer – Lidor shimoni May 25 '20 at 19:33
  • 1
    @candyDoesPrograms not 100% sure, but when doing `str()` you actually don't get the value, you get a representation of the object. Doing `print(s1Str)` should indicate this. There for, you need to get the actual value of the Entry object. That is by doing `.get()`. – Torxed May 25 '20 at 19:40
  • @candyDoesPrograms Read up on [The Variable Classes](http://effbot.org/tkinterbook/variable.htm) – stovfl May 25 '20 at 19:53

1 Answers1

2

A tk.Entry does not readily convert to a python integer. When you try str(tk.Entry), it will return '!entry'. When converting strings to integers, you can specify a base for the conversion, and int() will convert based on the base (pun unintended). For base 10, you have no letters or symbols, so it is not possible to convert '!entry' into an integer. You need to call tk.Entry().get(), which will return the string of anything in the entry. However, it will return this instantaneously, not giving the user time to input anything, so you will get an empty string, ''. Again, symbol 'nothing' is not in base 10, so it will raise an error. You need to put in a time delay before checking, to let the user input something, or you can make a button that calls a function that checks what you want to check when pressed:

def check():
    global s1, s2, s3
    s1 = s1E.get()
    s2 = s2E.get()
    s3 = s3E.get()

butt1 = tk.Button(text='Submit numbers', command=check)
...

This will check and assign all values in the entries when the user presses the button, giving him/her time to input.

User 12692182
  • 927
  • 5
  • 16