PythonSimpleGUI ValueError: could not convert string to float:

Viewed 35

I'm trying to write a simple application for personal finances with PythonSimpleGUI. When I try to access a value that's inputted by a user to preform a percentage formula on it I'm thrown the error ValueError: could not convert string to float:

To save time the problem is occuring at the if statement with the -SAVINGS1- element.

    import PySimpleGUI as sg
  

    layout = [
         [sg.Text("Budget your paycheck")],
         [sg.Text("Amount of money to budget"), sg.VSep(), sg.Input(key ="-BUDGET-"), 
    sg.Button("OK", key = "-BUDGET1-")],
         [sg.Text("Savings (%)"), sg.VSep(), sg.Input(key = "-SAVINGS-"), sg.Button("OK", 
    key = "-SAVINGS1-")], 
         [sg.Text("Disposable(%)"), sg.VSep(), sg.Input(key = "-DISPOSABLE-"), 
    sg.Button("OK", key = "-DISPOSABLE1-")],
         [sg.Text("Budget:"), sg.Text("Amount", key = "-BUDGETFINAL-")],
         [sg.Text("Savings:"), sg.Text("Amount", key = "-SAVINGSFINAL-")],
         [sg.Text("Disposable:"), sg.Text("Amount", key = "-DISPOSABLEFINAL-")]
    ]

    #create the window 
    window = sg.Window("Finance Calculator", layout)


    #create an event loop 
    while True:
    event, values = window.read()
    #end program if user closes window or 
    #presses the OK button
    if event == sg.WIN_CLOSED:
        break

    if event == "-BUDGET1-":
        window["-BUDGETFINAL-"].update(values["-BUDGET-"])
        budget = str(window["-BUDGET-"])
     
    if event == "-SAVINGS1-":
         savings = str(window["-SAVINGS-"]) 
         savings_p = (float(savings) // 100) * budget
         window["-SAVINGSFINAL-"].update(savings_p)

     

    window.close()

I assume the problem is happening because I'm trying to call float() on the window[] list and the string parsed through is "-SAVINGS1-" instead of the value that has been inputted by a user. I have tried entering the value into the input method as a float instead of an integer, but still get the same error. Is there a way for me to correctly get the inputted value, which is a number, using the float method?

Ideally the numeric value entered into the input field should be stored in the savings and budget variables so I can calculate them.

Thanks

1 Answers

Try this:

import PySimpleGUI as sg


layout = [
        [sg.Text("Budget your paycheck")],
        [sg.Text("Amount of money to budget"), sg.VSep(), sg.Input(key ="-BUDGET-"), 
sg.Button("OK", key = "-BUDGET1-")],
        [sg.Text("Savings (%)"), sg.VSep(), sg.Input(key = "-SAVINGS-"), sg.Button("OK", 
key = "-SAVINGS1-")], 
        [sg.Text("Disposable(%)"), sg.VSep(), sg.Input(key = "-DISPOSABLE-"), 
sg.Button("OK", key = "-DISPOSABLE1-")],
        [sg.Text("Budget:"), sg.Text("Amount", key = "-BUDGETFINAL-")],
        [sg.Text("Savings:"), sg.Text("Amount", key = "-SAVINGSFINAL-")],
        [sg.Text("Disposable:"), sg.Text("Amount", key = "-DISPOSABLEFINAL-")]
]

#create the window 
window = sg.Window("Finance Calculator", layout)


#create an event loop 
while True:
    event, values = window.read()
    #end program if user closes window or 
    #presses the OK button
    if event == sg.WIN_CLOSED:
        break

    if event == "-BUDGET1-":
        window["-BUDGETFINAL-"].update(values["-BUDGET-"])
        budget = str(values["-BUDGET-"])
    
    if event == "-SAVINGS1-":
        budget = str(values["-BUDGET-"])
        savings = str(values["-SAVINGS-"]) 
        savings_p = (float(savings) / 100) * float(budget)
        window["-SAVINGSFINAL-"].update(savings_p)

    
#window.close()
Related