pythonvariablesunbound

Unbound variable error when variable is already defined (Python)


I have a strange issue with this code. What this code does is create a GUI where I input a numeric value, then select what I want to convert it using the Spin element and finally I display the result by updating the output text with the outputString variable. When I convert from Kg to Pound it works fine, displaying the message it's supposed to display, when I convert from Seconds to Min, to displays the following error: "NameError: name 'outputString' is not defined", and when I convert from Km to Miles it gives me the same error of: "NameError: name 'outputString' is not defined". Any ideas what may be happening?

import PySimpleGUI as sg

layout = [
    [
        sg.Input(key="-INPUT-"), 
        sg.Spin("Km to Miles", "Kg to Pound", "Seconds to Min", key="-UNITS-"), 
        sg.Button("Convert", key="-CONVERT-")
    ],
    [sg.Text("Output", key="-OUTPUT-")]
]

window = sg.Window("Converter", layout)

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    if event == "-CONVERT-":
            inputValue = values["-INPUT-"]
            if inputValue.isnumeric():
                match values["-UNITS-"]:
                    case "Km to Miles":
                        output = round(float(inputValue) * 0.6254)
                        outputString = f"{inputValue} km  are {output} miles."
                    case "Kg to Pound":
                        output = round(float(inputValue) * 2.205)
                        outputString = f"{inputValue} kg  are {output} pounds."
                    case "Seconds to Min":
                        output = round(float(inputValue) / 60)
                        outputString = f"{inputValue} seconds  are {output} minutes."
                window["-OUTPUT-"].update(outputString)

window.close()

Solution

  • enter image description here

    Missing brackets in your Spin Element. The parameter is a list of choices.

    import PySimpleGUI as sg
    
    layout = [
        [
            sg.Input(key="-INPUT-"),
            sg.Spin(["Km to Miles", "Kg to Pound", "Seconds to Min"], readonly=True, key="-UNITS-"),
            sg.Button("Convert", key="-CONVERT-")
        ],
        [sg.Text("Output", key="-OUTPUT-")]
    ]
    
    window = sg.Window("Converter", layout)
    
    while True:
        event, values = window.read()
    
        if event == sg.WIN_CLOSED:
            break
    
        if event == "-CONVERT-":
                inputValue = values["-INPUT-"]
                if inputValue.isnumeric():
                    match values["-UNITS-"]:
                        case "Km to Miles":
                            output = round(float(inputValue) * 0.6254)
                            outputString = f"{inputValue} km  are {output} miles."
                        case "Kg to Pound":
                            output = round(float(inputValue) * 2.205)
                            outputString = f"{inputValue} kg  are {output} pounds."
                        case "Seconds to Min":
                            output = round(float(inputValue) / 60)
                            outputString = f"{inputValue} seconds  are {output} minutes."
                    window["-OUTPUT-"].update(outputString)
    
    window.close()
    

    If you're using an IDE, like PyCharm, then the docstrings will easily flag these kinds of mismatch of types errors: enter image description here