Code:
import PySimpleGUI as sg
layout = [
[sg.Input(s=(26, 1), background_color='red', k='Input')],
[sg.Button('White', s=(10, 1)), sg.Button('Black', s=(10, 1))],
]
window = sg.Window('test', layout=layout, margins=(1, 1))
while True:
event, values = window.read()
window.read()
if event == 'White':
window['Input'].update(background_color='white')
if event == 'Black':
window['Input'].update(background_color='black')
I made it so when you press a button the input field will change it's colour.
But why do I have to press the button twice to actually change it?
CodePudding user response:
Your event loop reads the event twice:
while True:
event, values = window.read()
window.read()
You only need it once.
Plus you should add an exit event.
Updated code:
layout = [
[sg.Input(s=(26, 1), background_color='red', k='Input')],
[sg.Button('White', s=(10, 1)), sg.Button('Black', s=(10, 1))],
]
window = sg.Window('test', layout=layout, margins=(1, 1))
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
if event == 'White':
window['Input'].update(background_color='white')
if event == 'Black':
window['Input'].update(background_color='black')