英文:
PySimpleGUI - Binding shortcut key to one element of a sub-layout
问题
感谢您阅读这篇文章。
我正在寻找一个标准解决方案,但找不到一个。
我有一个由两个布局构成的简单布局,第一个布局用于创建窗口。
我想要将快捷键绑定到右侧的按钮。
它们根本不接收事件,只有第一个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.
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("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
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("<Control_R><i>", "CTRL-i")
window.bind("<Control_R><o>", "CTRL-o")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论