user2993584
user2993584

Reputation: 139

Python - Tkinter Entry widget problems

Having a lot of trouble with this bit of code for my project, needing to do a simple multiplication with this code: Error is "Value Error: invalid literal for int() base 10: "

    def multcalc():
                        ans1=int(mEntry1.get())  #This is where it's locating the error
                        ans2=int(mEntry2.get()) #
                        print(ans1*ans2) 
                        return
                    multmenu=Tk()
                    mEntry1=StringVar()
                    mEntry2=StringVar()
                    multmenu.geometry('450x450+200+200')
                    multmenu.title('Multiplication')
                    input1msg=Label(text='Enter your first input').pack()
                    input1entry=Entry(multmenu,textvariable=mEntry1).pack()
                    input2msg=Label(text='Enter your second input').pack()
                    input2entry=Entry(multmenu,textvariable=mEntry2).pack() 
                    mCalculate=Button(multmenu,text='Enter',command=multcalc).pack()                               

Upvotes: 0

Views: 127

Answers (1)

ASGM
ASGM

Reputation: 11381

You're getting that error because you're trying to make a non-numeric string ('') into an integer. '' doesn't have an obvious numeric equivalent, so Python can't deal with it. The same would happen if mEntry.get() was 'salmon'.

Is '' an expected value for mEntry.get()? If so, maybe you need some specific logic to deal with it. For example, if you want '' to give you 0, you could do the following:

s = mEntry1.get()
if s != '':
    ans1 = 0
else:
    ans1 = int(s)

If '' isn't an expected value of mEntry.get(), then maybe the problem is earlier in your code.

Upvotes: 2

Related Questions