Python, pysimplegui, combine Text and ButtonMenu resulting in an array

Question:

I have a python recipe app utilizing pysimplegui. I want to fill in an ingredient and then have a dropdown for the amount of the ingredient, ie. 2 tbs, 1.5 tbs, 1 tbs, …etc.

Currently my layout is:

layout = [
    [sg.Text('Name', size=(15,1)), sg.InputText(key='dish_name')], 
    [sg.Text('Ingredient', size=(15,1)), sg.InputText(key='ingredient_name')],
    [sg.Submit(), sg.Exit()]
]

My amounts:

amounts = ['Amount', ['2', '1.5', '1', '.5', '3/4', '1/8']]

I would like to add to the layout:

[sg.ButtonMenu("Amount", amounts)]

in such a way that the selected amount would be in an array with the ingredient – for example:

{'dish_name': 'dish_name', 'ingredient': ['ingredient_name': ingredient_name, 'amount': amount] }

How do I do this? Any help is greatly appreciated. Thanks!!!

Asked By: Matt

||

Answers:

Example Code

import PySimpleGUI as sg

amounts = ['Amount', ['2', '1.5', '1', '.5', '3/4', '1/8']]

layout = [
    [sg.Text('Name', size=(15,1)), sg.InputText(key='dish_name')],
    [sg.Text('Ingredient', size=(15,1)), sg.InputText(key='ingredient_name')],
    [sg.Text('Amount', size=(15, 1)), sg.InputText(key='amount'), sg.ButtonMenu("Amount", amounts, key='amount_button')],
    [sg.Push(), sg.Submit(), sg.Exit(), sg.Push()]
]
window = sg.Window('Title', layout)

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    elif event in 'amount_button':
        window['amount'].update(values[event])
    elif event == 'Submit':
        dish_name = values['dish_name']
        ingredient_name = values['ingredient_name']
        amount = values['amount']
        data = {'dish_name': dish_name, 'ingredient': {'ingredient_name': ingredient_name, 'amount': amount}}
        print(data)

window.close()
Answered By: Jason Yang