简体   繁体   中英

Find an element inside a PySimpleGui's Frame by a key

How to find an element inside a PySimpleGui's Frame by a key without hardcoding it via .Rows[0] ?

import PySimpleGUI as sg

layout = [
    [sg.Frame(layout=[
        [sg.Text('Field:'), sg.Text('', key='key')],
    ])]
]

window = sg.Window("Name", layout)

You can use window['-KEY NAME-'] to find an element by it's key. Here is an example:

import PySimpleGUI as sg

layout = [
    [sg.Frame(title='My Frame', layout=[
        [sg.Text('Field:'), sg.Text('', key='-KEY NAME-', size=(10, 0))]])],
    [sg.Button('Go')] 
]

window = sg.Window(title='Name', layout=layout).finalize()

window['-KEY NAME-'].update('No value set')

# MAIN LOOP
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    if event == 'Go':
        window['-KEY NAME-'].update('Hello World!')

window.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM