-2

Trying to make the GUI application calculate cost. Unsure how to do it

So I've built my GUI Application using PySimpleGUI but i'd like to make it functional and calculate the total membership cost in the pop up window I've created. Can someone point me in the right direction? How do I apply numerical values to radio buttons and checkboxes to calculate costs?

Thanks! sorry I'm very new to this so I'm not well versed in Python.

  • It is very difficult to answer your question without seeing any of your data nor any of the code that you have written that produces your problem. Please edit your question to show a minimal reproducible set consisting of sample input, expected output, actual output, and only the relevant code necessary to reproduce the problem. See [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example "Minimal Reproducible Example") for details on how to best help us help you. – itprorh66 Jan 05 '23 at 14:13

1 Answers1

1

Most of time, I use dictionary to map the item to a value for calculation later. With enable_Events=True in Checkbox or Radio element, or another Submit Button, to generate an event when click for the calculation and GUI update.

import PySimpleGUI as sg

discounts = {"75%":0.75, "85%":0.85, "95%":0.95, "No":1.0}
prices = {"Summer Dress":545, "Coat":275, "Elegant Suit":685, "Casual Shirt":98, "Bridesmaid Dress":440, "Shoes":195}

layout = [
    [sg.Text("Discount")],
    [sg.Radio(discount, "Discount", enable_events=True, key=("Discount", discount)) for discount in discounts],
    [sg.Text("Price List")]] + [
    [sg.Checkbox(price, enable_events=True, key=("Price", price)), sg.Push(), sg.Text(prices[price])] for price in prices] + [
    [sg.Text("Total: 0", justification='right', expand_x=True, key='Total')],
]

window = sg.Window('Example', layout)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif isinstance(event, tuple) and event[0] in ('Discount', 'Price'):
        dis = 1
        for discount in discounts:
            if values[('Discount', discount)]:
                dis = discounts[discount]
                break
        total = sum([prices[price] for price in prices if values[("Price", price)]])
        window['Total'].update(f"Total: {int(total*dis)}")

window.close()

enter image description here

Jason Yang
  • 11,284
  • 2
  • 9
  • 23