不给出总数

问题描述 投票:0回答:1

所以,我正在使用

pysimplegui
开发 GUI,该项目是一个复利计算器。所以,当我点击完成按钮时,这么多年后它应该给我一个总数,但我只得到 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()

我尝试将浮标设置为

float(input(var name))
,但我仍然得到相同的结果。

python-3.x pysimplegui
1个回答
0
投票

所有

enable_events=True
元素都需要选项
Input
,这样你就可以在输入时获取事件,并且这些元素的值不会是初始值 0。

另一个问题是,在将输入元素转换为浮点数之前,您必须使用

values[key]
来获取输入元素的内容。

示例代码

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()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.