这是一个使用 tkinter gui 添加 2 个数字的小型 Python 程序。如果文本字段中有一些输入,它运行良好。如果输入为空白或数字以外的字符,如何处理导致的错误。
from tkinter import *
root = Tk()
class addtwo:
def evaluate(self, master, label, a, b):
label.configure(text="The result is" + str(a+b))
def __init__(self, master):
frame = Frame(master, width=300, height=200)
button1 = Button(master, text="Sum")
input1 = Entry(master, text="Text1")
input2 = Entry(master, text="Text2")
label = Label(master, text="")
button1.bind("<Button-1>", lambda event: self.evaluate(master, label,int(input1.get()),int(input2.get()) ))
input1.pack()
input2.pack()
frame.focus()
button1.pack()
label.pack()
frame.pack()
addtwo(root)
root.mainloop()
错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/__init__.py", line 1553, in __call__
return self.func(*args)
File "/home/temporary/PycharmProjects/practise/addtwo.py", line 21, in <lambda>
button1.bind("<Button-1>", lambda event: self.evaluate(master, label,int(input1.get()),int(input2.get()) ))
ValueError: invalid literal for int() with base 10: ''
慕村225694
相关分类