我在Python 2中制作了一个简单的程序,可以转换英寸,千克,磅和厘米。它在第一次运行时起作用,但之后尝试重新按1,2,3或4只会导致错误。我怎么能解决这个问题?
import Tkinter as tk
inch = 1
kilogram = 1
pound = 1
centimeter = 1
def keypress(event):
if event.keysym == 'Escape':
root.destroy()
x = event.char
if x == "1":
kilogram = .453592
pound = 1
input1 = float(raw_input('Please input a number in pounds. '))
print "A value of %r pound(s) is equal to %r kilogram(s)." % (input1, input1*kilogram)
elif x == "2":
kilogram = 1
pound = 2.20462
input1 = float(raw_input('Please input a number in kilograms. '))
print "A value of %r kilogram(s) is equal to %r pound(s)." % (input1, input1*pound)
elif x == "3":
inch = 1
centimeter = 2.54
input1 = float(raw_input('Please input a number in inches. '))
print "A value of %r inch(es) is equal to %r centimeter(s)." % (input1, input1*centimeter)
elif x == "4":
inch = .393701
centimeter = 1
input1 = float(raw_input('Please input a number in centimeters. '))
print "A value of %r centimeter(s) is equal to %r inch(es)." % (input1, input1*inch)
else:
print x
root = tk.Tk()
print "1=pounds to kilograms, 2=kilograms to pounds"
print "3=inches to centimeters,and 4=centimeters to inches. (Escape key to exit):"
root.bind_all('<Key>', keypress)
# don't show the tk window
root.withdraw()
root.mainloop()
如果我在运行一次后按1-4就会收到错误
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\Lib\lib-tk\Tkinter.py", line 1532, in __call__
return self.func(*args)
File "C:\Users\Noah Dasanaike\Desktop\converter.py", line 15, in keypress
input1 = float(raw_input('Please input a number in pounds. '))
ValueError: could not convert string to float:
答案 0 :(得分:0)
您收到此错误,因为您在每次按下时都会锁定按键。每次按1-4时,即使当前未完成,也会尝试创建新的raw_input。这引起了错误。
使用变量来判断输入正在使用中。
import Tkinter as tk
inch = 0.393701
kilogram = 0.453592
pound = 2.20462
centimeter = 2.54
USE = False
def keypress(event):
if event.keysym == 'Escape':
root.destroy()
if not USE:
handle(event.char)
def handle(char):
USE = True
if char is "1":
try:
lbs = raw_input('Please input a number in pounds. ')
lbs = float(lbs)
text = "A value of {0} pound(s) is equal to {1} kilogram(s).".format(lbs, lbs*kilogram)
print(text)
except ValueError:
pass
finally:
USE = False
return
elif char is "2":
try:
kgs = raw_input('Please input a number in kilograms. ')
kgs = float(kgs)
text = "A value of {0} kilogram(s) is equal to {1} pound(s).".format(kgs, kgs*pound)
print(text)
except ValueError:
pass
finally:
USE = False
return
elif char is "3":
try:
ins = raw_input('Please input a number in inches. ')
ins = float(ins)
text = "A value of {0} inch(es) is equal to {1} centimeter(s).".format(ins, ins*centimeter)
print(text)
except ValueError:
pass
finally:
USE = False
return
elif char is "4":
try:
cms = raw_input('Please input a number in centimeters. ')
cms = float(cms)
text = "A value of {0} centimeter(s) is equal to {1} inch(es).".format(cms, cms*inch)
print(text)
except ValueError:
pass
finally:
USE = False
return
print("1=pounds to kilograms, 2=kilograms to pounds")
print("3=inches to centimeters,and 4=centimeters to inches. (Escape key to exit):")
root = tk.Tk()
root.bind_all('<Key>', keypress)
root.mainloop()