Python - 无法重新输入

Python - Can't re-enter inputs

我正在 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\Desktop\converter.py", line 15, in keypress
    input1 = float(raw_input('Please input a number in pounds. '))
ValueError: could not convert string to float:

您收到此错误,因为您在每次按下时都绑定了按键。任何时候你按 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()