So, I'm working on a GUI using pysimplegui
the project is a compound interest calculator. So, when I hit the complete button, it should give me a total after so many years, but I just get 0.00.
# make a gui interest calc
import PySimpleGUI as sg
def main():
principle = 0
rate = 0
time = 0
# using 3 vars so the user can fill in to get
# the total of time and rate of interest
principleInputText = sg.Text("Enter principle amount: ")
principleInput = sg.InputText(key="principles")
rateInputText = sg.Text("Enter interest rate: ")
rateInput = sg.InputText(key='rates')
timeInputText = sg.Text("Enter time in years: ")
timeInput = sg.InputText(key='times')
complete_button = sg.Button("Complete")
output_label = sg.Text(key='output', text_color="black")
layout = [[principleInputText, principleInput],
[rateInputText, rateInput],
[timeInputText, timeInput],
[complete_button],
[output_label]]
# title of project
window = sg.Window(title="Compound Interest Calculator",
layout=layout)
while True:
event, values = window.read()
print(event, values)
match event:
case 'principles':
while principle <= 0:
principle = float(principleInput)
if principle <= 0:
print("principle cant be less or equal to 0")
case 'rates':
while rate <= 0:
rate = float(rateInput)
if rate <= 0:
print("Interest rate cant be less or equal to 0")
case 'times':
while time <= 0:
time = int(timeInput)
if time <= 0:
print("Time cant be less or equal to 0")
case 'Complete':
total = principle * pow((1 + rate / 100), time)
print(total)
window['output'].update(value=f'Balance after {time} year/s: ${total: .2f}')
# end program if user closes window
if event == "OK" or event == sg.WIN_CLOSED:
break
if __name__ == "__main__":
main()
I've tried to put the floats as float(input(var name))
, but I still get the same result.
Need option enable_events=True
for all your Input
elements, so you can get event when input, and the values for those elements won't be the initial value, 0.
Another issue is that you have to use values[key]
to get the content of input elements before you convert them into float.
Example Code
import PySimpleGUI as sg
def convert(string):
try:
value = float(string)
if value <= 0:
result, value = False, None
else:
result = True
except ValueError:
result, value = False, None
return result, value
items = [
["Amount", "Principle amount"],
["Rate", "Interest rate"],
["Times", "Time in years" ],
]
keys = [key for key, entry in items]
font = ("Courier New", 16)
sg.theme("DarkBlue")
sg.set_options(font=font)
layout = [
[sg.Text(entry), sg.Push(), sg.Input(size=30, key=key)] for key, entry in items] + [
[sg.Text("", expand_x=True, justification="center", key="Output")],
[sg.Push(), sg.Button("Submit"),sg.Push()],
]
window = sg.Window(title="Compound Interest Calculator", layout=layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == "Submit":
data = []
for key in keys:
ok, value = convert(values[key])
if not ok:
sg.popup(f"The value of {key} entered is wrong !", button_justification="center")
break
data.append(value)
else:
principle, rate, time = data
total = principle * pow((1 + rate / 100), time)
window['Output'].update(f'Balance after {time} year/s: ${total: .2f}')
window.close()