Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...