英文:
PysimpleGui: pre-select an Item in a Combobox on Keyboard input
问题
I have a GUI with PySimpleGUI that uses a combobox.
有一个使用PySimpleGUI的GUI,其中使用了一个组合框。
There are alphabetically sorted strings in the list for the combobox.
组合框的列表中有按字母顺序排序的字符串。
The Combobox is readonly (to only input the strings available), so the user gets the drop-down list right away when clicking anywhere on the combobox element.
组合框是只读的(只能输入可用的字符串),因此用户在单击组合框元素的任何位置时立即获得下拉列表。
When the drop-down list is open, the user can highlight items by the "up" and "down" keys. Also, selection of the highlighted item by the enter key is possible.
当下拉列表打开时,用户可以使用"上"和"下"键来突出显示项目。还可以使用回车键选择突出显示的项目。
It would increase the usability if the highlight jumps to (or selects) the entry that starts with "S" when pushing the "s" key on the keyboard.
如果在键盘上按下"s"键时突出显示跳到(或选择)以"S"开头的条目,将增加可用性。
What I tried:
我尝试过:
-
opening the window with
return_keyboard_events=True
but it would not change the behavior. -
使用
return_keyboard_events=True
打开窗口,但不会改变行为。 -
Fiddling with the
enable_per_char_events=True
for the combo element just shows that this method is no longer available in version 4.60.4 (though it is still in the docs) and the code crashes. -
尝试在组合元素上使用
enable_per_char_events=True
只是显示出该方法在版本4.60.4中已不再可用(尽管它仍在文档中),并且代码崩溃。
英文:
I have a GUI with pysimplegui that uses a combobox.
There are alphabetically sorted strings in the list for the combobox.
The Combobox is readonly (to only input the strings available),
so the user gets the drop down list right away when clicking anywhere on the combobox element.
When the drop-down list is open, the user can highlight items by the "up" and "down" keys.
Also selection of the highlighted item by the enter key is possible.
It would increase the usability, if the highlight jumps to (or selects) the entry that starts with S when pushing the "s" key on the keyboard.
What I tried
-
opening the window with
return_keyboard_events=True
but it would not change the behaviour. -
Fiddeling with the
enable_per_char_events=True
for the combo element just shows that this method is no longer available in version 4.60.4 (though it is still in the docs) and the code crashes.
Screenshot
答案1
得分: 1
> 4.61.0.85
>
> Combo Element - 新参数 enable_per_char_events。当为True时,将在输入单个字符时获取事件。
请从GitHub升级您的PySimpleGUI。
并非所有增强功能都添加到PyPI版本中。
从GitHub下载
> PySimpleGUI包中有代码,可将您以前使用pip安装的包升级到GitHub中的最新版本。
> 您可以使用命令 psgmain
运行测试工具或者使用 psgupgrade
调用GitHub升级代码。
或者,您可以调用 sg.main()
,如下所示
d:\>python
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import PySimpleGUI as sg
>>> sg.main()
<br>
以下代码演示了另一种方法,通过绑定实现,但这里需要tkinter代码。
import PySimpleGUI as sg
lst = sorted([
'Global', 'USA', 'India', 'Brazil', 'Russian', 'France', 'UK', 'Italy',
'Spain', 'Germany', 'Colombia', 'Argentina', 'Mexico', 'Turkey', 'Poland',
'Iran', 'Ukraine', 'South Africa', 'Peru', 'Netherlands'])
width = max(map(len, lst)) + 2
font = ("Courier New", 16)
sg.theme("DarkBlue3")
sg.set_options(font=font)
layout = [[sg.Combo(lst, size=(width, 5), key='COMBO'), sg.Button('Exit')]]
window = sg.Window("COMBO", layout, finalize=True)
combo = window['COMBO']
combo.bind('<Key>', ' Key')
combo.bind('<Enter>', ' Enter')
user_event = False
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, 'Exit'):
break
elif event == 'COMBO Enter':
combo.Widget.select_range(0, 'end')
elif event == 'COMBO Key':
entry = values['COMBO'].lower()
if user_event:
user_event = False
else:
if entry:
index = None
for i, item in enumerate(lst):
if item.lower().startswith(entry):
index = i
break
if index is not None:
user_event = True
combo.Widget.set(lst[index])
combo.Widget.event_generate('<Key-Down>')
combo.Widget.current(index)
print(event, values)
window.close()
英文:
> 4.61.0.85
>
> Combo Element - new parameter enable_per_char_events. When True will get an event when individual characters are entered.
Please upgrade your PySimpleGUI from Github.
Not all enhancements added into the PyPI version.
Download from GitHub
>There is code in the PySimpleGUI package that upgrades your previously pip installed package to the latest version checked into GitHub.
> You can use the commands psgmain
to run the test harness or psgupgrade
to invoke the GitHub upgrade code.
or, you can call sg.main()
, like
d:\>python
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import PySimpleGUI as sg
>>> sg.main()
<br>
Following code demo another way by binding for it, but tkinter code required here.
import PySimpleGUI as sg
lst = sorted([
'Global', 'USA', 'India', 'Brazil', 'Russian', 'France', 'UK', 'Italy',
'Spain', 'Germany', 'Colombia', 'Argentina', 'Mexico', 'Turkey', 'Poland',
'Iran', 'Ukraine', 'South Africa', 'Peru', 'Netherlands'])
width = max(map(len, lst)) + 2
font = ("Courier New", 16)
sg.theme("DarkBlue3")
sg.set_options(font=font)
layout = [[sg.Combo(lst, size=(width, 5), key='COMBO'), sg.Button('Exit')]]
window = sg.Window("COMBO", layout, finalize=True)
combo = window['COMBO']
combo.bind('<Key>', ' Key')
combo.bind('<Enter>', ' Enter')
user_event = False
while True:
event, values = window.read()
if event in (sg.WINDOW_CLOSED, 'Exit'):
break
elif event == 'COMBO Enter':
combo.Widget.select_range(0, 'end')
elif event == 'COMBO Key':
entry = values['COMBO'].lower()
if user_event:
user_event = False
else:
if entry:
index = None
for i, item in enumerate(lst):
if item.lower().startswith(entry):
index = i
break
if index is not None:
user_event = True
combo.Widget.set(lst[index])
combo.Widget.event_generate('<Key-Down>')
combo.Widget.current(index)
print(event, values)
window.close()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论