-3

I wrote some simple code to describe my problem: I want to take an integer value from an entry to use it later.I tried also to use a spin box. here is my code:

from tkinter import*       
win=Tk()
win.geometry('300x200')
e=Entry(width=10)
e.pack()
y=int(e.get())
print(y*2)

I always get the same error:

y = int(e.get())
ValueError: invalid literal for int() with base 10 ' '

I don't know why this is happening!

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • there is several problems in your example, for example. in which moment you are trying to get value from Entry? – ncica Apr 10 '20 at 10:55
  • at every time i write a value. or maybe when i click on enter –  Apr 10 '20 at 11:30

2 Answers2

1

ValueError: invalid literal for int() with base 10 ' '

means that you are trying to convert the string "" to an integer. This, of course, is invalid. The reason you are trying to convert an empty string to an integer is that you are not allowing any value to be placed into the entry.

A good way to allow this to happen would be using a button, which calls a function to get the value within the entry and print it. Also, you are missing the line win.mainloop() which you would need at the end of your code.

Here is the example code you are probably asking for:

from tkinter import *

win = Tk()
win.geometry('300x200')

def printVal():
    y = int(e.get())
    print(y*2)

e = Entry(width=10)
e.pack()
b = Button(text="push me to print the value in e")
b.pack()

win.mainloop()

This code will still return errors if the value in the entry is not a valid integer, so if you want it to be robust, you'll have to play around with it.

ofthegoats
  • 295
  • 4
  • 10
  • thanks a lot bro, so the problem is that i need a specefic moment where the program will turn it into a integer ? your button is missing the ``command=printVal` btw . –  Apr 10 '20 at 11:26
  • @RayenSayadi Not so much a specific moment when the program will turn it into an integer, so much as when the program will get the value. You can do whatever you want with it then. The other problem was that there was no point in the program the user could put a value into entry for it to be taken. – ofthegoats Apr 10 '20 at 11:28
1

there is several problems in your example, for example. in which moment you are trying to get value from Entry?

your code should looks something like:

from tkinter import *


def validate(value):
    print (value)
    try:
        if value:
            return int(value)
    except:
        return None

def calculate():
    x = e1.get()
    x_validate = validate(x)

    if x_validate == None:
        e1.delete(0, END)
        e1.insert(0, 'you need to enter integer')
        Label1.config(text='')
    else:
        result = x_validate*2
        Label1.config(text=result)


win = Tk()

e1 = Entry(win)
e1.grid(row=0, column=0)

Button(win,text='Calculate', command = calculate).grid(row=0, column=1)
Label(win,text='Result').grid(row=1, column=0)
Label1 = Label(win)
Label1.grid(row=1, column=1)


win.mainloop()

Example if you enter integer and press calculate

enter image description here

Example if you enter string and press calculate

enter image description here

ncica
  • 7,015
  • 1
  • 15
  • 37