我正在开发一个关于Tkinter的简单GUI。我想要的是将所有填充的空白加起来,如果有的话。如果有空字段,只需将它们设为“0”。
然而,它没有成功,问题在于:
value2 = float(c.get())
错误是:
ValueError: could not convert string to float:
以下是代码:
from Tkinter import *
import ttk
def plus(*args):
value = float(a.get())
value1 = float(b.get())
if c:
value2 = float(c.get())
else:
value2 == float(0.0)
if d:
value3 = float(d.get())
else:
value3 == float(0.0)
result.set(value + value1 + value2 + value3)
root = Tk()
root.title("Plus them")
mainframe = ttk.Frame(root, padding="10 10 10 10")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
a = StringVar()
b = StringVar()
c = StringVar()
d = StringVar()
result = StringVar()
feet_entry = ttk.Entry(mainframe, width=5, textvariable=a)
feet_entry.grid(column=2, row=1, sticky=(W, E))
feet_entry1 = ttk.Entry(mainframe, width=5, textvariable=b)
feet_entry1.grid(column=5, row=1, sticky=(W, E))
feet_entry2 = ttk.Entry(mainframe, width=5, textvariable=c)
feet_entry2.grid(column=2, row=2, sticky=(W, E), pady = 5)
feet_entry3 = ttk.Entry(mainframe, width=5, textvariable=d)
feet_entry3.grid(column=5, row=2, sticky=(W, E), pady = 5)
ttk.Label(mainframe, text="the result is").grid(column=3, row=3, sticky=W)
ttk.Label(mainframe, textvariable = result).grid(column=5, row=3, sticky=(W, E))
ttk.Button(mainframe, text="Plus", command=plus).grid(column=3, row=4, sticky=W)
feet_entry.focus()
root.bind('<Return>', plus)
root.mainloop()
我认为问题出在功能定义部分。我在下面尝试过:
def plus(*args):
try:
value = float(a.get())
value1 = float(b.get())
value2 = float(c.get())
value3 = float(d.get())
result.set(value + value1 + value2 + value3)
except ValueError:
pass
但它没有帮助。什么是正确的方法?
答案 0 :(得分:2)
c.get()并不总是返回类似“0.02”的内容,如果没有用户输入,它将返回“”,在这种情况下,它将抛出异常,显示错误以将“”传递给float。所以我修改了你的代码,添加了一些东西来检查用户输入。
from Tkinter import *
import ttk
def inputToFloat(input):
if isinstance(input, float):
return input
elif isinstance(input, basestring):
input = input.strip()
return 0.0 if not input else float(input)
else:
raise Exception("unsupported type")
def plus(*args):
value = inputToFloat(a.get())
value1 = inputToFloat(b.get())
if c:
value2 = inputToFloat(c.get())
else:
value2 == inputToFloat(0.0)
if d:
value3 = inputToFloat(d.get())
else:
value3 == inputToFloat(0.0)
result.set(value + value1 + value2 + value3)
root = Tk()
root.title("Plus them")
mainframe = ttk.Frame(root, padding="10 10 10 10")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
a = StringVar()
b = StringVar()
c = StringVar()
d = StringVar()
result = StringVar()
feet_entry = ttk.Entry(mainframe, width=5, textvariable=a)
feet_entry.grid(column=2, row=1, sticky=(W, E))
feet_entry1 = ttk.Entry(mainframe, width=5, textvariable=b)
feet_entry1.grid(column=5, row=1, sticky=(W, E))
feet_entry2 = ttk.Entry(mainframe, width=5, textvariable=c)
feet_entry2.grid(column=2, row=2, sticky=(W, E), pady = 5)
feet_entry3 = ttk.Entry(mainframe, width=5, textvariable=d)
feet_entry3.grid(column=5, row=2, sticky=(W, E), pady = 5)
ttk.Label(mainframe, text="the result is").grid(column=3, row=3, sticky=W)
ttk.Label(mainframe, textvariable = result).grid(column=5, row=3, sticky=(W, E))
ttk.Button(mainframe, text="Plus", command=plus).grid(column=3, row=4, sticky=W)
feet_entry.focus()
root.bind('<Return>', plus)
root.mainloop()
答案 1 :(得分:0)
根据fhdrsdg的建议,它改为以下并且有效。
def plus(*args):
value = float(a.get())
value1 = float(b.get())
if c.get():
value2 = float(c.get())
else:
value2 = 0.0
if d.get():
value3 = float(d.get())
else:
value3 = 0.0
result.set(value + value1 + value2 + value3)