PySimpleGUI – 将快捷键绑定到子布局的一个元素

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

PySimpleGUI - Binding shortcut key to one element of a sub-layout

问题

感谢您阅读这篇文章。

我正在寻找一个标准解决方案,但找不到一个。
我有一个由两个布局构成的简单布局,第一个布局用于创建窗口。

PySimpleGUI – 将快捷键绑定到子布局的一个元素

我想要将快捷键绑定到右侧的按钮。
它们根本不接收事件,只有第一个InputText元素一直接收事件。
我需要InputText元素也接收事件,我已经实现了这一点。
我需要这种布局架构用于更复杂的GUI(标签元素等)。

我已经准备了一个包含我的问题的脚本。

import PySimpleGUI as Sg

input_folder_layout = [Sg.Text("input folder",
                               size=(20, 1),
                               background_color="#64778D"),
                       Sg.InputText(default_text="",
                                    key="input_folder",
                                    size=(65, 1),
                                    enable_events=True),
                       Sg.FolderBrowse("...",
                                       size=(6, 1),
                                       key="input_folder_browse",
                                       enable_events=True)]
output_folder_layout = [Sg.Text("output folder", size=(20, 1),
                                background_color="#64778D"),
                        Sg.InputText(default_text="",
                                     key="output_folder",
                                     size=(65, 1),
                                     enable_events=True),
                        Sg.FolderBrowse("...", 
                                        size=(6, 1),
                                        key="output_folder_browse",
                                        enable_events=True)]

ui_layout = [input_folder_layout, output_folder_layout]

Sg.ChangeLookAndFeel("DarkBlue2")
window = Sg.Window("UI Test", ui_layout, finalize=True)

window.bind("<Ctrl_R><i>", "CTRL-i")
window.bind("<Ctrl_R><o>", "CTRL-o")

while True:
    event, values = window.read(timeout=100)

    if event != "__TIMEOUT__" and event is not None:
        if event in ("input_folder_browse", "CTRL-i"):
            print("input folder browse event found")

        if event in ("output_folder_browse", "CTRL-o"):
            print("output folder browse event found")

    if event == Sg.WINDOW_CLOSED:
        break

    window.refresh()

window.close()
window = None

我正在使用PySimpleGUI==4.60.3和Python==3.9.13。

谢谢。

英文:

Thank you for reading this.

I am searching for a standard solution but couldn't find one.<br>
I have a simple layout made out of two layouts and the first layout is the one used to create the window.

PySimpleGUI – 将快捷键绑定到子布局的一个元素

I want to bind shorcut keys to only the right buttons.<br>
They are not receiving the events at all, only always the first InputText element.<br>
I need to have the InputText elements also receiving events and I could achieved that already.<br>
I need that kind of layouts' architecture for a more complex GUI (Tab element and so on).<br>

I have prepared one script that can contain my problem.

import PySimpleGUI as Sg

input_folder_layout = [Sg.Text(&quot;input folder&quot;,
                               size=(20, 1),
                               background_color=&quot;#64778D&quot;),
                       Sg.InputText(default_text=&quot;&quot;,
                                    key=&quot;input_folder&quot;,
                                    size=(65, 1),
                                    enable_events=True),
                       Sg.FolderBrowse(&quot;...&quot;,
								       size=(6, 1),
								       key=&quot;input_folder_browse&quot;,
								       enable_events=True)]
output_folder_layout = [Sg.Text(&quot;output folder&quot;, size=(20, 1),
							    background_color=&quot;#64778D&quot;),
                        Sg.InputText(default_text=&quot;&quot;,
								     key=&quot;output_folder&quot;,
								     size=(65, 1),
								     enable_events=True),
                        Sg.FolderBrowse(&quot;...&quot;, 
                                        size=(6, 1),
									    key=&quot;output_folder_browse&quot;,
									    enable_events=True)]

ui_layout = [input_folder_layout, output_folder_layout]

Sg.ChangeLookAndFeel(&quot;DarkBlue2&quot;)
window = Sg.Window(&quot;UI Test&quot;, ui_layout, finalize=True)

window.bind(&quot;&lt;Ctrl_R&gt;&lt;i&gt;&quot;, &quot;CTRL-i&quot;)
window.bind(&quot;&lt;Ctrl_R&gt;&lt;o&gt;&quot;, &quot;CTRL-o&quot;)

while True:
    event, values = window.read(timeout=100)

    if event != &quot;__TIMEOUT__&quot; and event is not None:
	    if event in (&quot;input_folder_browse&quot;, &quot;CTRL-i&quot;):
		    print(&quot;input folder browse event found&quot;)
		
	    if event in (&quot;output_folder_browse&quot;, &quot;CTRL-o&quot;):
		    print(&quot;output folder browse event found&quot;)

	if event == Sg.WINDOW_CLOSED:
	    break

    window.refresh()

window.close()
window = None

I am using PySimpleGUI==4.60.3 and Python==3.9.13.

Thank you.

答案1

得分: 1

"ctrl"键的绑定是"Control",而不是"ctrl"。

window.bind("<Control_R><i>", "CTRL-i")
window.bind("<Control_R><o>", "CTRL-o")
英文:

The binding for ctrl key is Control, not ctrl.

window.bind(&quot;&lt;Control_R&gt;&lt;i&gt;&quot;, &quot;CTRL-i&quot;)
window.bind(&quot;&lt;Control_R&gt;&lt;o&gt;&quot;, &quot;CTRL-o&quot;)

huangapple
  • 本文由 发表于 2023年2月6日 16:36:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358989.html
匿名

发表评论

匿名网友

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

确定