您好我使用Python 3.4创建了一个图形计算器,直到我尝试使用加减按钮输入加法或减法函数。我收到以下错误:
ValueError: invalid literal for int() with base 10: ''
据我所知,错误与将输入转换为整数有关,但我无法解决问题!提前谢谢。
import sys
from tkinter import*
total= 0
temp= 0
temp2= 0
unisymbol=' '
def equalparttwo():
global total
global temp
global temp2
global unisymbol
if unisymbol== 'minus' :
temp2=int(textoutput.get())
total=temp-temp2
textoutput.delete(0,END)
textoutput.insert(END,total)
elif unisymbol=='plus':
temp2=int(textoutput.get())
total=temp + textoutput.get()
textoutput.delete(0,END)
textoutput.insert(END,total)
def add():
global temp
global unisymbol
temp=int(textoutput.get())
unisymbol='plus'
textoutput.delete(0,END)
def sub():
global temp
global unisymbol
temp=int(textoutput.get())
unisymbol='minus'
textoutput.delete(0,END)
def equal():
global temp2
temp2=int(textoutput.get())
textoutput.delete(0,END)
equalparttwo()
def clear():
textoutput.delete(0,END)
def nine():
textoutput.insert(END,9)
def eight():
textoutput.insert(END,8)
def seven():
textoutput.insert(END,7)
def six():
textoutput.insert(END,6)
def five():
textoutput.insert(END,5)
def four():
textoutput.insert(END,4)
def three():
textoutput.insert(END,3)
def two():
textoutput.insert(END,2)
def one():
textoutput.insert(END,1)
def zero():
textoutput.insert(END,0)
root = Tk()
frame=Frame(root)
frame.pack()
root.title("Calculator")
num1=StringVar()
topframe=Frame(root)
topframe.pack(side=TOP)
textoutput=Entry(frame,textvariable=num1,bd=20, insertwidth=1,font=30,bg="pink")
textoutput.pack(side=TOP)
button1=Button(topframe,padx=16,pady=16,bd=6, text="7",fg="black",bg="pink",command=seven)
button1.pack(side=LEFT)
button2=Button(topframe,padx=16,pady=16,bd=6, text="8",fg="black",bg="pink",command=eight)
button2.pack(side=LEFT)
button3=Button(topframe,padx=16,pady=16,bd=6, text="9",fg="black",bg="pink",command=nine)
button3.pack(side=LEFT)
button4=Button(topframe,padx=16,pady=16,bd=6, text="-",fg="black",bg="pink",command=sub)
button4.pack(side=LEFT)
frame1=Frame(root)
frame1.pack(side=TOP)
button1=Button(frame1,padx=16,pady=16,bd=6, text="6",fg="black",bg="pink",command=six)
button1.pack(side=LEFT)
button2=Button(frame1,padx=16,pady=16,bd=6, text="5",fg="black",bg="pink",command=five)
button2.pack(side=LEFT)
button3=Button(frame1,padx=16,pady=16,bd=6, text="4",fg="black",bg="pink",command=four)
button3.pack(side=LEFT)
button4=Button(frame1,padx=16,pady=16,bd=6, text="+",fg="black",bg="pink",command=add)
button4.pack(side=LEFT)
frame2=Frame(root)
frame2.pack(side=TOP)
button1=Button(frame2,padx=16,pady=16,bd=6, text="1",fg="black",bg="pink",command=one)
button1.pack(side=LEFT)
button2=Button(frame2,padx=16,pady=16,bd=6, text="2",fg="black",bg="pink",command=two)
button2.pack(side=LEFT)
button3=Button(frame2,padx=16,pady=16,bd=6, text="3",fg="black",bg="pink",command=three)
button3.pack(side=LEFT)
button4=Button(frame2,padx=16,pady=16,bd=6, text="C",fg="black",bg="pink",command=clear)
button4.pack(side=LEFT)
frame3=Frame(root)
frame3.pack(side=TOP)
button1=Button(frame3,padx=16,pady=16,bd=6, text=temp,fg="black",bg="pink")
button1.pack(side=LEFT)
button2=Button(frame3,padx=16,pady=16,bd=6, text="0",fg="black",bg="pink",command=zero)
button2.pack(side=LEFT)
button3=Button(frame3,padx=16,pady=16,bd=6, text=total,fg="black",bg="pink")
button3.pack(side=LEFT)
button4=Button(frame3,padx=16,pady=16,bd=6, text="=",fg="black",bg="pink",command=equal)
button4.pack(side=LEFT)
root.mainloop()
答案 0 :(得分:2)
错误表示您在emty字符串(int()
)上运行''
;这显然对Python没有意义......
因此,您希望在所有int(textoutput.get())
位置添加支票,执行以下操作:
def equal():
global temp2
value = textoutput.get()
try:
temp2 = int(value)
except ValueError:
# Show some warning that you're unable to parse this value here.
# This error also occurs is someone filled in 'banana' or some
# other value that can't possibly be made into an int ...
show_warning()
return false
textoutput.delete(0,END)
equalparttwo()
我使用value
变量来确保ValueError
来自
int(value)
,而我们并未发现其他(意外)错误
textoutput.get()
(这可能很难调试)。
这里的教训是永远不会信任用户输入;你在这里期待一个数字, 但用户输入可以是任何。即使是简单的,也要始终清理用户输入 像计算器这样的目的,并且从不对它能做什么做出假设 不可能。
答案 1 :(得分:2)
问题在于:
def equalparttwo():
# ...
if unisymbol == 'minus':
temp2 = int(textoutput.get())
在此行,textoutput
为空;因为你已经在调用者中收集了输入并将其删除了:
def equal():
global temp2
temp2=int(textoutput.get()) # Here you already have saved value
textoutput.delete(0,END) # and here you have cleared it
equalparttwo()
因此,您可以在temp2 = int(textoutput.get())
equalparttwo()