pySimpleGUI – Set Cursor and refresh
Question:
Using pySimpleGUI I simply want to change the mouse cursor while the code is processing a query, but the cursor does only change after the code is executed… it does work when I put a window.read() after the set_cursor() call, but then the proceeding code is not executed immediately. What am I doing wrong? Unfortunately, the documentation does not provide an example.
import PySimpleGUI as sg
#... more code
window = sg.Window(f"Report Generator v{APP_VERSION}", layout)
while True:
event, values = window.read()
# close button was clicked
if event == "Close" or event == sg.WIN_CLOSED:
break
# Generate button was clicked
elif event == "Generate":
window.set_cursor("coffee_mug")
window.refresh()
# more code where the cursor should be changed but isn't
Answers:
Following code work on my WIN10
import PySimpleGUI as sg
layout = [
[sg.Text("Hello World")],
[sg.Button('Submit'), sg.Button('Cancel')],
]
window = sg.Window(f"Title", layout)
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Cancel'):
break
elif event == "Submit":
window.set_cursor("coffee_mug")
window.close()
Not completely sure why is that, but the following works as desired. The crucial part is to add the Submit button ID at the window['Submit'].set_cursor("coffee_mug")
, then the new cursor is displayed before the next code is executed. And it will also show the new cursor at the entire window, not only when hovering over the button as one might have expected:
import PySimpleGUI as sg
layout = [
[sg.Text("Hello World")],
[sg.Button('Submit'), sg.Button('Cancel')],
]
window = sg.Window(f"Report Generator v{APP_VERSION}", layout)
while True:
event, values = window.read()
# close button was clicked
if event == "Close" or event == sg.WIN_CLOSED:
break
# Generate button was clicked
elif event == "Submit":
window['Submit'].set_cursor("coffee_mug")
time.sleep(5)
# more code where the cursor should be changed but isn't
I have a window that connects to a database and I disable the button and change the button text to show that it is "Working". When the task is finished I enable the button and change the text back to "Connect to Database". I think this is very similar to what you are trying to do.
Here is my function:
def dbConnectWin(iniConfig, user='', winTitle='Database User Name', pwd=''):
sg.theme(iniConfig.read('MasterWin', 'window-theme'))
layout = [[sg.Text('User:', size=(10, 1)), sg.InputText(user, key='User', size=(30, 1))],
[sg.Text('Password:', size=(10, 1)), sg.InputText(pwd, key='Password', password_char='*', size=(30, 1))],
[sg.Radio('Local Lan', "Host", default=True, key='LanHost'),
sg.Radio('VPN-ing', "Host", key='VPNHost'),
sg.Radio('Wan or Remote', "Host", key='WanHost')],
[sg.Button(button_text='Connect to Database', key='ConnectButton', bind_return_key=True),
sg.Button('Exit Program', key='Cancel')]]
window = sg.Window(winTitle, layout, element_justification='center',
location=iniConfig.read('MasterWin', 'window-position'), icon=dataBaseIcon, finalize=True, keep_on_top=True)
window.force_focus()
window.bring_to_front()
window['ConnectButton'].set_focus()
while True:
event, values = window.read(timeout=50)
if event != '__TIMEOUT__':
print("DBlogin Event", event)
if event == 'ConnectButton':
if values['LanHost']:
host = iniConfig.read('GLOBAL', 'lan-host-name')
elif values['WanHost']:
host = iniConfig.read('GLOBAL', 'wan-host-name')
elif values['VPNHost']:
host = iniConfig.read('GLOBAL', 'vpn-host-name')
else:
sg.popup("Program Error - Lan & Wan Host was False", location=centerChildWindow(window),
title="Program Error", keep_on_top=True, modal=True, auto_close=True)
sys.exit(1)
window['ConnectButton'].update(disabled=True, text='Working')
window.refresh()
con = databaseConnect("Database", values['User'], values['Password'], host, window)
if con != -1:
window['ConnectButton'].update(disabled=True, text='Connected')
window.refresh()
break
else:
window['ConnectButton'].update(disabled=False, text='Connect to Database')
window.refresh()
elif event in ('Cancel', sg.WIN_CLOSED):
sys.exit(1)
window.close()
del window
iniConfig.dbUser = values['User'] # set global variable dbUser of the user that validated loging into database
iniConfig.conDB = con
return
Using pySimpleGUI I simply want to change the mouse cursor while the code is processing a query, but the cursor does only change after the code is executed… it does work when I put a window.read() after the set_cursor() call, but then the proceeding code is not executed immediately. What am I doing wrong? Unfortunately, the documentation does not provide an example.
import PySimpleGUI as sg
#... more code
window = sg.Window(f"Report Generator v{APP_VERSION}", layout)
while True:
event, values = window.read()
# close button was clicked
if event == "Close" or event == sg.WIN_CLOSED:
break
# Generate button was clicked
elif event == "Generate":
window.set_cursor("coffee_mug")
window.refresh()
# more code where the cursor should be changed but isn't
Following code work on my WIN10
import PySimpleGUI as sg
layout = [
[sg.Text("Hello World")],
[sg.Button('Submit'), sg.Button('Cancel')],
]
window = sg.Window(f"Title", layout)
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Cancel'):
break
elif event == "Submit":
window.set_cursor("coffee_mug")
window.close()
Not completely sure why is that, but the following works as desired. The crucial part is to add the Submit button ID at the window['Submit'].set_cursor("coffee_mug")
, then the new cursor is displayed before the next code is executed. And it will also show the new cursor at the entire window, not only when hovering over the button as one might have expected:
import PySimpleGUI as sg
layout = [
[sg.Text("Hello World")],
[sg.Button('Submit'), sg.Button('Cancel')],
]
window = sg.Window(f"Report Generator v{APP_VERSION}", layout)
while True:
event, values = window.read()
# close button was clicked
if event == "Close" or event == sg.WIN_CLOSED:
break
# Generate button was clicked
elif event == "Submit":
window['Submit'].set_cursor("coffee_mug")
time.sleep(5)
# more code where the cursor should be changed but isn't
I have a window that connects to a database and I disable the button and change the button text to show that it is "Working". When the task is finished I enable the button and change the text back to "Connect to Database". I think this is very similar to what you are trying to do.
Here is my function:
def dbConnectWin(iniConfig, user='', winTitle='Database User Name', pwd=''):
sg.theme(iniConfig.read('MasterWin', 'window-theme'))
layout = [[sg.Text('User:', size=(10, 1)), sg.InputText(user, key='User', size=(30, 1))],
[sg.Text('Password:', size=(10, 1)), sg.InputText(pwd, key='Password', password_char='*', size=(30, 1))],
[sg.Radio('Local Lan', "Host", default=True, key='LanHost'),
sg.Radio('VPN-ing', "Host", key='VPNHost'),
sg.Radio('Wan or Remote', "Host", key='WanHost')],
[sg.Button(button_text='Connect to Database', key='ConnectButton', bind_return_key=True),
sg.Button('Exit Program', key='Cancel')]]
window = sg.Window(winTitle, layout, element_justification='center',
location=iniConfig.read('MasterWin', 'window-position'), icon=dataBaseIcon, finalize=True, keep_on_top=True)
window.force_focus()
window.bring_to_front()
window['ConnectButton'].set_focus()
while True:
event, values = window.read(timeout=50)
if event != '__TIMEOUT__':
print("DBlogin Event", event)
if event == 'ConnectButton':
if values['LanHost']:
host = iniConfig.read('GLOBAL', 'lan-host-name')
elif values['WanHost']:
host = iniConfig.read('GLOBAL', 'wan-host-name')
elif values['VPNHost']:
host = iniConfig.read('GLOBAL', 'vpn-host-name')
else:
sg.popup("Program Error - Lan & Wan Host was False", location=centerChildWindow(window),
title="Program Error", keep_on_top=True, modal=True, auto_close=True)
sys.exit(1)
window['ConnectButton'].update(disabled=True, text='Working')
window.refresh()
con = databaseConnect("Database", values['User'], values['Password'], host, window)
if con != -1:
window['ConnectButton'].update(disabled=True, text='Connected')
window.refresh()
break
else:
window['ConnectButton'].update(disabled=False, text='Connect to Database')
window.refresh()
elif event in ('Cancel', sg.WIN_CLOSED):
sys.exit(1)
window.close()
del window
iniConfig.dbUser = values['User'] # set global variable dbUser of the user that validated loging into database
iniConfig.conDB = con
return