Python GUI计算器-ValueError:以10为基数的int()的文本无效:'';

Python GUI计算器-ValueError:以10为基数的int()的文本无效:'';,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,您好,我已经使用Python3.4创建了一个图形计算器,它一直工作到我尝试使用add和subtract按钮输入add或subtract函数为止。我收到以下错误: ValueError: invalid literal for int() with base 10: '' 我知道错误与将输入转换为整数有关,但我无法得出答案!提前谢谢 import sys from tkinter import* total= 0 temp= 0 temp2= 0 unisymbol=' ' def

您好,我已经使用Python3.4创建了一个图形计算器,它一直工作到我尝试使用add和subtract按钮输入add或subtract函数为止。我收到以下错误:

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()

错误表示您正在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()
(这可能很难调试)

这里的教训是永远不要相信用户的输入;你在等一个数字, 但是用户输入可以是任何内容。始终清理用户输入,即使是简单的 就像计算器一样,永远不要对它能做什么和做什么假设
不能是。

错误表明您正在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()
(这可能很难调试)

这里的教训是永远不要相信用户的输入;你在等一个数字, 但是用户输入可以是任何内容。始终清理用户输入,即使是简单的 就像计算器一样,永远不要对它能做什么和做什么假设 不可能。问题出在这里:

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()
因此,您可以在
equalparttwo()中删除这一行
temp2=int(textoutput.get())
,问题在于:

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()

因此,您可以在
equalparttwo()

中删除这一行
temp2=int(textoutput.get())
,错误来自哪一行?