如何将 right_click_menu 与多个 InputText 元素一起使用?

How to use right_click_menu with multiple InputText elements?

在PySimpleGUI中使用right_click_menu时如何区分同一个window上的多个元素?例如,对于下面的代码,如何判断我正尝试将 right_click_menu 与两个 InputText 元素中的哪一个一起使用?如果我将某些内容复制到剪贴板,然后在其中一个输入字段上右键单击 'Paste',则两个字段中都会出现相同的数据。当我右键单击其中一个 InputText 字段时,如何编写代码来识别我在哪个字段上?:

import PySimpleGUI as sg

INPUT1 = 'INPUT1'
INPUT2 = 'INPUT2'

right_click_menu = ['',['Paste']]

layout = [
    [sg.Text('Input1'), sg.InputText('', key='INPUT1', right_click_menu = right_click_menu)],
    [sg.Text('Input2'), sg.InputText('', key='INPUT2', right_click_menu = right_click_menu)],
    [sg.Button(' OK '), sg.Button(' Exit ')]
    ]

window = sg.Window('Multiple Elements', layout)

input1:sg.InputText = window[INPUT1]
input2:sg.InputText = window[INPUT2]

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

    if event in (' Exit ', None):
        break

    if event == 'Paste':
        # How to tell whether I am right-clicking on INPUT1 or INPUT2?
        # With just one Input element, I could just do this: 

        input1.Widget.insert(sg.tk.INSERT, window.TKroot.clipboard_get())

        # What do I do when there is a second InputText field? 
        # Below won't work because I'll get the same text pasted into both fields.

        input2.Widget.insert(sg.tk.INSERT, window.TKroot.clipboard_get())



    if event == ' OK ':
        pass
        #Do blah

window.close()

参考https://pysimplegui.readthedocs.io/en/latest/#keys-for-menus

A key is indicated by adding :: after a menu entry, followed by the key.

import PySimpleGUI as sg

INPUT1 = 'INPUT1'
INPUT2 = 'INPUT2'

right_click_menu = [['',[f'Paste::Paste {i}']] for i in range(2)]

layout = [
    [sg.Text('Input1'), sg.InputText('', key='INPUT1', right_click_menu = right_click_menu[0])],
    [sg.Text('Input2'), sg.InputText('', key='INPUT2', right_click_menu = right_click_menu[1])],
    [sg.Button(' OK '), sg.Button(' Exit ')]
    ]

window = sg.Window('Multiple Elements', layout)

input1:sg.InputText = window[INPUT1]
input2:sg.InputText = window[INPUT2]

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

    if event in (' Exit ', None):
        break

    if event.startswith('Paste'):
        element = input1 if event.split()[1] == '0' else input2
        element.Widget.insert(sg.tk.INSERT, window.TKroot.clipboard_get())

window.close()