0

I want to make a calculator that adds 10 to the number using tkinter. But I had to get the entry price in int format, so I used get method. I keep getting the following error message. What's the problem?

from tkinter import *

window=Tk()



a=Entry(window)
a.pack()
b=a.get()
c=int(b)

result2=Label(window,text="")
result2.pack()

def Button_Press():
    global c
    result=c+10
    result2.config(text=result)

button=Button(window,command=Button_Press())
button=pack()


window.mainloop()

Here's the error text.

c=int(b)
ValueError: invalid literal for int() with base 10: ''
Eugene
  • 27
  • 4

1 Answers1

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

Has show you that the c is ''(a string which length is zero).The code seems to be int('').That's why it will raise exception. Your code could be:

from tkinter import *

window = Tk()

a = Entry(window)
a.insert("end", "0") # just insert a str "0"
a.pack()
b = a.get()
c=int(b)
print(c)

result2 = Label(window, text="")
result2.pack()


def Button_Press():
    result = c + 10
    result2.config(text=result)


button = Button(window, command=Button_Press) # there is no "()"
button.pack() # not button = pack()

window.mainloop()
jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
  • The `a.get()` and conversion to int should happen inside the Button_Press function rather than only only as the start of the code when the Entry widget is empty. – scotty3785 Jun 04 '20 at 10:21