Skip to content Skip to sidebar Skip to footer

No Idea Why I Am Getting "valueerror: Invalid Literal For Int() With Base10: '' "

window = tk. Tk() #creates a new window age = StringVar() window.title('Are you old enough to smoke?') #title window.geometry('300x200') #window size window.wm_iconbitmap('favic

Solution 1:

As @tdelaney says in its comment, the error message says that the StringVar age is empty.

You can test that age contains an int representation (via a regex) before trying to convert it to an int

import re
intre = re.compile("\d+")
...
if intre.match(age.get):
    ageint = int(age.get())
    ...
else:
    # warn the user for invalid input

You can also use the optimistic way: it should be good, and just test exceptional conditions

...
try:
    ageint = int(age.get())
except ValueError:
    # warn the user for invalid input

In that case, you would better use directly an IntVar for age:

age = IntVar()
...
try:
    ageint = age.get()
...

But if you want to make sure that user can only type digits in the Entry, you should use validate and validatecommand options. Unfortunately they are poorly documented in Tkinter world, and the best reference I could find are 2 other SO posts, here and here

Solution 2:

This could make your program more stable:

def callback():
    try:
        ageint = int(float(age.get()))
    except ValuError:
        mess = "Please enter a valid number."
        messagebox.showinfo(title="Invalid Input", message=mess)
        returnif ageint >= 18:
    # rest of your code

Post a Comment for "No Idea Why I Am Getting "valueerror: Invalid Literal For Int() With Base10: '' ""