pySimpleGUI 和 Python

pySimpleGUI e Python

我需要一些帮助。

我正在训练这段代码:

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

我想在 Combo 列表中选择的值,它的键是 ='-CATEG-',填入 key = '-NAME-'。但是返回的是对象而不是所选值,例如:。 还有一件事:你能将键:'-CATEG-' + '-BRAND-' + '-COLOR-' 与这个连接点放在 key = '- NAME-' 中连接吗?示例:在 'Category' 组合中,选择了智能手机选项;在“品牌、摩托罗拉和 'Color '”中,黑色。因此,'Name' 字段应为:Smartphone Motorola Black。

此外,创建变量来定义一些参数是一个很好的做法,就像对变量 'size_font' 所做的那样?我是这么认为的,因为我相信维护会更容易。

  1. 通过 values[key] 获取所选值,而不是 window[key]
window['-NAME-'].update(values['-CATEG-'])
  1. 使用方法str.join连接所有字符串
text = ' '.join([values['-CATEG-'], values['-BRAND-'], values['-COLOR-']])
window['-NAME-'].update(text)
  1. 在使用元素之前设置默认选项
size_font = ("Courier New", 20)
sg.set_options(font=size_font)