使用pySimpleGUI,我只是想在代码处理查询时更改鼠标游标,但是游标只在代码执行之后才会改变.当我在window.read()调用之后放置一个set_cursor()时,它确实有效,但随后不会立即执行该代码。我做错了什么?不幸的是,文档没有提供一个示例。
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发布于 2022-10-28 11:46:03
不完全确定原因是什么,但下面的工作,如所需。关键的部分是在window['Submit'].set_cursor("coffee_mug")中添加提交按钮ID,然后在执行下一段代码之前显示新的游标。它还将在整个窗口显示新的光标,而不仅仅是像人们所期望的那样在按钮上盘旋时:
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发布于 2022-10-28 10:42:45
下面的代码工作在我的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()发布于 2022-10-30 03:12:51
我有一个连接到数据库的窗口,我禁用按钮并更改按钮文本以显示它“工作”。任务完成后,我启用按钮并将文本更改为“连接到数据库”。我认为这和你想要做的非常相似。
这是我的功能:
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
returnhttps://stackoverflow.com/questions/74232601
复制相似问题