Joshua Day
Joshua Day

Reputation: 43

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('favicon.ico')  #icon

photo = tk.PhotoImage(file="images.png")  #picture in said window
w = tk.Label(window, image=photo)
w.pack()

lbl=tk.Label(window, text="Please enter your age.", bg="light salmon", fg="blue2")  #label text & color
lbl.pack()

ent = tk.Entry(window, text="(Your age here)", textvariable=age)
ent.pack()


def callback():
    ageint = int(age.get())
#Line causing error RIP^
    if ageint >= 18:
        mess = 'You are legally able to smoke cigarettes.'
    elif ageint <= 12:
        mess = "You're to young to smoke,\nGet out of here."
    else:
        mess = "You are not of legal age to smoke."

    if ageint >= 21:
        mess += "\nYou are legally able to smoke marijuana."
    if ageint >= 40:
        mess += "\nYou're above the age of forty,\nDo you really need to ask 

    messagebox.showinfo(title="Can you Smoke", message=mess)

btn = tk.Button(window, text="Confirm", bg="sienna1", fg="blue2", relief="groove", command=callback)
btn.pack()

There is my code. Every thing works great when I run my program (very simple little practice thing) it sometimes work and displays the correct message. but sometimes my messagebox does not show up and this error appears in the console:

Exception in Tkinter callback
    Traceback (most recent call last):
      File "C:\Python34\lib\tkinter\__init__.py", line 1533, in __call__
        return self.func(*args)
      File "C:/Users/Josh/Documents/Trial Python Programs/Smoke window Program.py", line 22, in callback
        ageint = int(age.get(), base=3)
    ValueError: invalid literal for int() with base 10: ''

I have found many topics relating to the issue, but none that specifically help me (or atleast that I understand). I am just confused because sometimes it happens and sometimes it does not. Any help/tips would be greatly appreciated.

Background info:

  1. Python34

  2. Win7-64 bit

  3. I've been doing this for less than a week go easy on me please.

  4. I only provided the amount of code that I thought was needed.

Upvotes: 0

Views: 1583

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 149145

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

Upvotes: 2

Mike M&#252;ller
Mike M&#252;ller

Reputation: 85542

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)
        return
    if ageint >= 18:
    # rest of your code

Upvotes: 1

Related Questions