简体   繁体   English

PySimpleGUI 列表框右键菜单

[英]PySimpleGUI Listbox right click menu

I am wondering, is it possible to make the right click menu work on the selected box in the list box?我想知道,是否可以使右键菜单在列表框中的选定框上起作用?

Example of a right click menu右键菜单示例

右键菜单示例

I have succeeded in making a right click menu for the entire list box by doing我已经成功地为整个列表框制作了一个右键菜单

layout = [
        [gui.Listbox(size=(35, 22), key='chat', values=messages,
                     right_click_menu=['&Right', ['Delete', 'Favourite', 'Reply', 'Copy', 'Edit']])],
        [gui.InputText(key='input', size=(25, 10)), gui.Button('Send', bind_return_key=True, size=(9, 1))]
    ]

but its not quite what I am looking for.但它并不完全是我想要的。 I want: if I right click on a box in the list box, said box will be selected and the actions of that menu will affect only that box.我想要:如果我右键单击列表框中的一个框,该框将被选中,并且该菜单的操作将仅影响该框。 Lets say I right click on the middle box and press delete, the middle box will be deleted.假设我右键单击中间框并按删除,中间框将被删除。 I know how to work with the event of clicking the menu, but as of right now I have no way of actually telling which message was clicked.我知道如何处理单击菜单的事件,但到目前为止,我无法真正知道单击了哪条消息。

It's possible, but need tkinter code and hack PySimleGUI code.这是可能的,但需要 tkinter 代码和破解 PySimleGUI 代码。 Here new callback for Button 3, but selection for nearest one, not exact the one.这里是按钮 3 的新回调,但选择最近的一个,而不是精确的那个。 Maybe sg.Table or sg.Tree can achieve better selection.也许sg.Tablesg.Tree可以实现更好的选择。

import PySimpleGUI as sg

def RightClickMenuCallback(event, element):
    widget = element.Widget
    current = widget.curselection()
    if current:
        widget.selection_clear(current[0])
    index = widget.nearest(event.y)
    widget.selection_set(index)
    element.TKRightClickMenu.tk_popup(event.x_root, event.y_root, 0)
    element.TKRightClickMenu.grab_release()

messages = [
    'This is the start of your chat!',
    'demo messages',
    'dont know what to right',
]

command = ['Delete', 'Favourite', 'Reply', 'Copy', 'Edit']
layout = [
    [sg.Listbox(size=(35, 22), key='chat', values=messages,
        right_click_menu=['&Right', command])],
    [sg.InputText(key='input', size=(25, 10)),
     sg.Button('Send', bind_return_key=True, size=(9, 1))],
]
window = sg.Window("Test", layout, finalize=True)
chat = window['chat']
chat.Widget.bind('<Button-3>', lambda event,
    element=chat: RightClickMenuCallback(event, element))

while True:

    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    print(event, values)

window.close()

[Edit] [编辑]

IMO, follow the way provided by library should be preferred. IMO,应首选图书馆提供的方式。

import PySimpleGUI as sg

messages = [
    'This is the start of your chat!',
    'demo messages',
    'dont know what to right',
]

command = ['Delete', 'Favourite', 'Reply', 'Copy', 'Edit']
cmd_layout = [[sg.Button(cmd, size=(10, 1))] for cmd in command]
layout = [
    [sg.Listbox(values=messages, size=(35, 22), key='chat'),
     sg.Column(cmd_layout)],
    [sg.InputText(key='input', size=(25, 10)),
     sg.Button('Send', bind_return_key=True, size=(9, 1))],
]
window = sg.Window("Test", layout, finalize=True)
window['input'].expand(expand_x=True)

while True:

    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    print(event, values)

window.close()

Add option enable_events=True to sg.Listbox if you need event generated when mouse button 1 clicked.如果您需要在单击鼠标按钮 1 时生成事件,请将选项enable_events=True添加到sg.Listbox When any event generated, you can get the list of items currently selected in this listbox by values['chat'] or window['chat'].get() , now there's only one item selected, so message is values['chat'][0] .当任何事件产生时,您可以通过values['chat']window['chat'].get()此列表框中当前选择的项目列表,现在只有一个项目被选择,所以消息是values['chat'][0]

If you need index, you have to call window['chat'].get_indexes() , it returns the items currently selected as a list of indexes.如果需要索引,则必须调用window['chat'].get_indexes() ,它将当前选择的项目作为索引列表返回。 So, the same, get index of selected item by window['chat'].get_indexes()[0]因此,同样,通过window['chat'].get_indexes()[0]获取所选项目的索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM