1

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()
Jecs
  • 7
  • 6
  • Please include the full traceback error. – ewokx May 12 '22 at 03:18
  • `outputString` will only be defined if that statement runs. If `inputValue` is not numeric, or if the spin button is set to anything other than "Km to Miles", then `outputString` will be unbound. – Tim Roberts May 12 '22 at 03:19
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community May 12 '22 at 06:47

3 Answers3

0

outputString is currently only scoped inside the map statement, so you need to declare it outside of the match statement.

if event == "-CONVERT-":
        inputValue = values["-INPUT-"]
        if inputValue.isnumeric():
            outputString = ""
            match values["-UNITS-"]:
                case "Km to Miles":
                    output = round(float(inputValue) * 0.6254)
                    outputString = f"{inputValue} km  are {output} miles."

            window["-OUTPUT-"].update(outputString)

Now the problem is if no case gets matched outputString will be empty. Think about what needs to happen if no case is matched (perhaps add a default case case _: that throws an exception or writes a default value).

Eric Breyer
  • 720
  • 1
  • 10
  • @Eirc I have made the modifications you suggested to my code, and while the error has disappeared, now whenever the value gets updated it always returns an empty string. – Jecs May 12 '22 at 04:31
  • outputString is initialized as an empty string in my example (probably not the best implementation but I don't fully know what you want). This means that if no cases get matched, outputString will still be the empty string. I do not know what test you are using, but I suspect nothing matches your single case statement and outputString remains the empty string. – Eric Breyer May 12 '22 at 04:37
  • I have updated the question with the complete code and a more detailed explanation of what is happening. – Jecs May 12 '22 at 13:48
0

Apparently, the error was not due to the variable, but rather an error coming from the sg.Spin element of PySimpleGUI, as whenever it is clicked, the name changes, thus there was no valid case.

Jecs
  • 7
  • 6
  • The error not come from the Spin element, but the condition code missed for all the cases in your code, then you got exception `NameError: name 'outputString' is not defined` when the value of Spin is out of your cases. – Jason Yang May 12 '22 at 15:26
0

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

Mike from PSG
  • 5,312
  • 21
  • 39