PysimpleGui:在键盘输入时预选Combobox中的项目。

huangapple go评论56阅读模式
英文:

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中已不再可用(尽管它仍在文档中),并且代码崩溃。

Screenshot:
截图:PysimpleGui:在键盘输入时预选Combobox中的项目。

英文:

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

PysimpleGui:在键盘输入时预选Combobox中的项目。

答案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()

PysimpleGui:在键盘输入时预选Combobox中的项目。

<br>

以下代码演示了另一种方法,通过绑定实现,但这里需要tkinter代码。

import PySimpleGUI as sg

lst = sorted([
    &#39;Global&#39;, &#39;USA&#39;, &#39;India&#39;, &#39;Brazil&#39;, &#39;Russian&#39;, &#39;France&#39;, &#39;UK&#39;, &#39;Italy&#39;,
    &#39;Spain&#39;, &#39;Germany&#39;, &#39;Colombia&#39;, &#39;Argentina&#39;, &#39;Mexico&#39;, &#39;Turkey&#39;, &#39;Poland&#39;,
    &#39;Iran&#39;, &#39;Ukraine&#39;, &#39;South Africa&#39;, &#39;Peru&#39;, &#39;Netherlands&#39;])
width = max(map(len, lst)) + 2

font = (&quot;Courier New&quot;, 16)
sg.theme(&quot;DarkBlue3&quot;)
sg.set_options(font=font)

layout = [[sg.Combo(lst, size=(width, 5), key=&#39;COMBO&#39;), sg.Button(&#39;Exit&#39;)]]
window = sg.Window(&quot;COMBO&quot;, layout, finalize=True)
combo = window[&#39;COMBO&#39;]
combo.bind(&#39;&lt;Key&gt;&#39;, &#39; Key&#39;)
combo.bind(&#39;&lt;Enter&gt;&#39;, &#39; Enter&#39;)
user_event = False

while True:
    event, values = window.read()
    if event in (sg.WINDOW_CLOSED, &#39;Exit&#39;):
        break
    elif event == &#39;COMBO Enter&#39;:
        combo.Widget.select_range(0, &#39;end&#39;)
    elif event == &#39;COMBO Key&#39;:
        entry = values[&#39;COMBO&#39;].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(&#39;&lt;Key-Down&gt;&#39;)
                    combo.Widget.current(index)

    print(event, values)

window.close()

PysimpleGui:在键盘输入时预选Combobox中的项目。

英文:

> 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:\&gt;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 &quot;help&quot;, &quot;copyright&quot;, &quot;credits&quot; or &quot;license&quot; for more information.
&gt;&gt;&gt; import PySimpleGUI as sg
&gt;&gt;&gt; sg.main()

PysimpleGui:在键盘输入时预选Combobox中的项目。

<br>

Following code demo another way by binding for it, but tkinter code required here.

import PySimpleGUI as sg

lst = sorted([
    &#39;Global&#39;, &#39;USA&#39;, &#39;India&#39;, &#39;Brazil&#39;, &#39;Russian&#39;, &#39;France&#39;, &#39;UK&#39;, &#39;Italy&#39;,
    &#39;Spain&#39;, &#39;Germany&#39;, &#39;Colombia&#39;, &#39;Argentina&#39;, &#39;Mexico&#39;, &#39;Turkey&#39;, &#39;Poland&#39;,
    &#39;Iran&#39;, &#39;Ukraine&#39;, &#39;South Africa&#39;, &#39;Peru&#39;, &#39;Netherlands&#39;])
width = max(map(len, lst)) + 2

font = (&quot;Courier New&quot;, 16)
sg.theme(&quot;DarkBlue3&quot;)
sg.set_options(font=font)

layout = [[sg.Combo(lst, size=(width, 5), key=&#39;COMBO&#39;), sg.Button(&#39;Exit&#39;)]]
window = sg.Window(&quot;COMBO&quot;, layout, finalize=True)
combo = window[&#39;COMBO&#39;]
combo.bind(&#39;&lt;Key&gt;&#39;, &#39; Key&#39;)
combo.bind(&#39;&lt;Enter&gt;&#39;, &#39; Enter&#39;)
user_event = False

while True:
    event, values = window.read()
    if event in (sg.WINDOW_CLOSED, &#39;Exit&#39;):
        break
    elif event == &#39;COMBO Enter&#39;:
        combo.Widget.select_range(0, &#39;end&#39;)
    elif event == &#39;COMBO Key&#39;:
        entry = values[&#39;COMBO&#39;].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(&#39;&lt;Key-Down&gt;&#39;)
                    combo.Widget.current(index)

    print(event, values)

window.close()

PysimpleGui:在键盘输入时预选Combobox中的项目。

huangapple
  • 本文由 发表于 2023年5月13日 23:13:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243447.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定