0
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.

Joshua Day
  • 43
  • 1
  • 9
  • I belive the problem is `int(age.get())` if it gets a decimal it gives the same error you're getting – depperm Dec 12 '15 at 05:08
  • If you put in a decimal in the entry shouldn't it as a normal number? Like if I put 18.5 wouldnt it just see it as half way between 18 and 19? (That is just logic in my head, nothing to do with actual knowledge of how the code works). If it does not, is there a way to make it so it does? I also don't understand why it would still only happen sometimes when you enter a whole number. – Joshua Day Dec 12 '15 at 05:13
  • could you try changing `int()` to `float()` and see if that works – depperm Dec 12 '15 at 05:16
  • As I posted that I got an error again. this time it says ValueError: could not convert string to float: – Joshua Day Dec 12 '15 at 05:19
  • look at http://stackoverflow.com/questions/8420143/valueerror-could-not-convert-string-to-float-id – depperm Dec 12 '15 at 05:23
  • I want to say I was able to fix my problem with it, but I was not. – Joshua Day Dec 12 '15 at 06:30
  • 2
    The error message gives you a good hint: `ValueError: invalid literal for int() with base 10: ''`. Those empty quotes at the end is the empty string you tried to convert to an int. So, your program is operating normally, it just caught bad input from the user (in this case, no data was entered). Catch the exception and show a "Try following instructions" message box. – tdelaney Dec 12 '15 at 06:43
  • The error does not only happen when you do not put anything in the entry box. you can put in a full entry box and the error will still happen. What seems to be happening is when I open the program, right there it decides if it is going to reply to everything with an error. – Joshua Day Dec 12 '15 at 06:54

2 Answers2

2

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

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
1

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
Mike Müller
  • 82,630
  • 20
  • 166
  • 161