Carlos Henrique
Carlos Henrique

Reputation: 3

pySimpleGUI e Python

I would like some help.

I'm training on this code:

    import PySimpleGUI as sg

    category = ['Smartphone', 'Battery', 'Charger']
    brand = ['Iphone', 'Motorola', 'LG']
    color = ['White', 'Green', 'Black']
    size_font = 20

    layout = [[sg.Text('Code', font=size_font), sg.Input(key='-COD-', font=size_font, size=(20, 1))],
              [sg.Text('Un', font=size_font), sg.InputText(key='-UN-', font=size_font, size=(10, 1))],
              [sg.Text('Name', font=size_font), sg.Input(key='-NAME-', size=(30, 1))],
              [sg.Text('Category', font=size_font), sg.Combo(category, font=size_font, key='-CATEG-', size=(30, 1))],
              [sg.Text('Brand', font=size_font), sg.Combo(marca, font=size_font, key='-BRAND-')],
              [sg.Text('Color', font=size_font), sg.Combo(color, font=size_font, key='-COL-')],
              [sg.Text('')],
              [sg.Button('Insert', font=size_font), sg.Button('Cancel', font=size_font)]]

    window = sg.Window('Product Registration', layout, size=(700, 300))

    while True:
        event, values = window.read()
        if event in (sg.WIN_CLOSED, 'Cancel'):
            break
        if event == 'Insert':
            window['-NAME-'].update(window['-CATEG-'])


    window.close()

I would like the value chosen in the Combo list, whose key is ='-CATEG-' , was filled in key = '-NAME-'. But the object is returning and not the chosen value, example: <PySimpleGUI.PySimpleGUI.Combo object at 0x7fd8bf982a60>. And another thing: Can you concatenate the keys: '-CATEG-' + '-BRAND-' + '-COLOR-' with this junction being placed in key = '- NAME-' ?. Example: In the 'Category' Combo, the Smartphone option was chosen; in 'Brand, Motorola and 'Color ', Black. Thus, the 'Name' field should be: Smartphone Motorola Black.

In addition, it is a good practice to create variables to define some parameters, as done for the variable 'size_font'? I thought so because I believe that maintenance will be easier.

Upvotes: 0

Views: 182

Answers (1)

Jason Yang
Jason Yang

Reputation: 13051

  1. Get selected value by values[key], not window[key]
window['-NAME-'].update(values['-CATEG-'])
  1. Use method str.join to concate all strings
text = ' '.join([values['-CATEG-'], values['-BRAND-'], values['-COLOR-']])
window['-NAME-'].update(text)
  1. set default options before use elements
size_font = ("Courier New", 20)
sg.set_options(font=size_font)

Upvotes: 1

Related Questions