Search code examples
pythonvariablespysimplegui

How to use value/key of an input in pysimplegui to make calculations


I've been trying to use my input from a text to make some calculations with it, save it as another variable and then print it. Here is the code:

import PySimpleGUI as sg

def fixedDepositCalc1():
    
    layout = [
        [sg.Text("What is your principle amount?:")],
        [sg.I(key='-PRINCIPLE-')],
        [sg.Text("What is your interest rate for your deposit? Please give your answer in a percent form:")],
        [sg.I(key='-RATE-')],
        [sg.Text("What is the tenure for your fixed deposit?:")],
        [sg.I(key='-TIME-')],
        [sg.B("Ok")]
    ]

    window4 = sg.Window("Fixed Deposit Calculator - Financial Manager").Layout(layout)
    event, values = window4.read()

    if event == "Ok":
        I_MONEY = float(values('-PRINCIPLE-')) * float((values('-RATE-')/100)) * float(values('TIME'))
        P_MONEY = float(values('-PRINCPLE-')) + I_MONEY
        global principle
        principle = float(values='-PRINCIPLE-')

    

    layout2 = [
        [sg.T(f"Your principle amount is{principle}")]
    ]

    window5 = sg.Window("Fixed Deposit Calculator - Financial Manager").Layout(layout2)
    event, values = window5.read()

And here are the errors that pop-up

File "c:\Users\Kashyap\Desktop\pysimplegui\fixedDepositCalc.py", line 19, in fixedDepositCalc1
    I_MONEY = float(values('-PRINCIPLE-')) * float((values('-RATE-')/100)) * float(values('TIME'))
TypeError: 'dict' object is not callable

I tried to save the values variable as another variable so that I can be able to work with it.


Solution

  • values is dict type, so you should use [xxx] to get the value, and then there are some small problems in your code, I corrected it for you, you can try it

    import PySimpleGUI as sg
    
    
    def fixedDepositCalc1():
        layout = [
            [sg.Text("What is your principle amount?:")],
            [sg.I(key="-PRINCIPLE-")],
            [
                sg.Text(
                    "What is your interest rate for your deposit? Please give your answer in a percent form:"
                )
            ],
            [sg.I(key="-RATE-")],
            [sg.Text("What is the tenure for your fixed deposit?:")],
            [sg.I(key="-TIME-")],
            [sg.B("Ok")],
        ]
    
        window4 = sg.Window("Fixed Deposit Calculator - Financial Manager").Layout(layout)
        event, values = window4.read()
        if event == "Ok":
            I_MONEY = (
                float(values["-PRINCIPLE-"])
                * (float(values["-RATE-"]) / 100)
                * float(values["-TIME-"])
            )
            P_MONEY = float(values["-PRINCIPLE-"]) + I_MONEY
            global principle
            principle = float(values["-PRINCIPLE-"])
    
        layout2 = [[sg.T(f"Your principle amount is{principle}")]]
    
        window5 = sg.Window("Fixed Deposit Calculator - Financial Manager").Layout(layout2)
        event, values = window5.read()