Search code examples
pythontkinterkeypress

Python - Can't re-enter inputs


I'm making a simple program in Python 2 that converts inches, kilograms, pounds, and centimeters. It works on the first run, but attempting to re-press 1, 2, 3, or 4 afterwards just results in an error. How could I fix this?

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

The error received if I press 1-4 after running this once is

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:

Solution

  • You're getting this error, because you've bound the keys on every press. Any time you press 1-4 it tries to create a new raw_input, even if the current isn't complete. Which raises an error.

    Use a variable to tell when an input is in use.

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