PYSimpleGUI does not generate FOCUSOUT event when pushing a button

143 Views Asked by At
import PySimpleGUI as sg
def dialog():
    layout = [[sg.Text("Value1:",size=(5,1)), sg.I(key="-VALUE1-",size=(10,1),focus=True)],
              [sg.Text("Value2:", size=(5, 1)), sg.I(key="-VALUE2-", size=(10, 1),focus=True)],
              [sg.Text("",size=(5,1)),sg.OK(key="-OK-"),sg.B("Show",key="-SHOW-"),sg.Cancel(key="-CANCEL-")]]
    window = sg.Window("Inputdialog", layout,modal=True,finalize=True)
    window.Element("-VALUE1-").SetFocus()
    window["-VALUE1-"].bind('<FocusOut>', 'FOCUS OUT')
    window["-VALUE2-"].bind('<FocusOut>', 'FOCUS OUT')
    while True:
        event, values = window.read()
        print(event)
        if event in (sg.WIN_CLOSED, "-CANCEL-"):
            break
        if event in ("-SHOW-"):
            print(values)
        if event in ("-OK-"):
            return values
    window.close()

result = dialog()

When i tab from one field to another, a Focusout is generated. But when i'm in an inputfield and push a button, the Focusout of that inputfield is not generated. How can the Focusout be forced anyway?

1

There are 1 best solutions below

0
On

Default tk Button won't steal focus when clicked, but steal focus for ttk Button.

import PySimpleGUI as sg

layout = [
    [sg.Input(key='-IN1-')],
    [sg.Input(key='-IN2-')],
    [sg.Button('TK OK'), sg.Button('TTK OK', use_ttk_buttons=True)],
]

window = sg.Window('Title', layout, finalize=True)
for key in ('-IN1-', '-IN2-'):
    window[key].bind('<FocusOut>', ' FocusOut')

while True:

    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break
    print(event)

window.close()