PySimpleGUI / PySimpleGUI

Python GUIs for Humans! PySimpleGUI is the top-rated Python application development environment. Launched in 2018 and actively developed, maintained, and supported in 2024. Transforms tkinter, Qt, WxPython, and Remi into a simple, intuitive, and fun experience for both hobbyists and expert users.

Home Page:https://www.PySimpleGUI.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] For a Tree, how do I simulate right_key_selects=True?

rtl19 opened this issue · comments

commented

Type of Issue (Question)


Operating System

Windows 10

PySimpleGUI Port (tkinter, Qt, Wx, Web)

tkinter


Versions

4.60.0

Python version (sg.sys.version)

3.8

PySimpleGUI Version (sg.__version__)

4.60.0

GUI Version (tkinter (sg.tclversion_detailed), PySide2, WxPython, Remi)


Your Experience In Months or Years (optional)

Years Python programming experience: 3

Years Programming experience overall: 20+

Have used another Python GUI Framework? (tkinter, Qt, etc) (yes/no is fine): no

Anything else you think would be helpful?


Troubleshooting

These items may solve your problem. Please check those you've done by changing - [ ] to - [X]

  • [ X] Searched main docs for your problem www.PySimpleGUI.org
  • [ X] Looked for Demo Programs that are similar to your goal Demos.PySimpleGUI.org
  • [ X] If not tkinter - looked for Demo Programs for specific port
  • For non tkinter - Looked at readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
  • [ X] Run your program outside of your debugger (from a command line)
  • [ X] Searched through Issues (open and closed) to see if already reported Issues.PySimpleGUI.org
  • Tried using the PySimpleGUI.py file on GitHub. Your problem may have already been fixed but not released

Detailed Description

Since sg.Tree doesn't appear to have the option 'right_click_selects', I'm looking for the simplest code to do the same thing.

Full Disclosure: I saw another thread where someone was able to do this, but the code was doing a bunch of other stuff in addition that I didn't understand. I wasn't able to distill the lines of code needed to just do the right_click_selects for a Tree.

Hopefully right_click_selects option can be added to sg.Tree in the future.

Code To Duplicate

N/A

# Paste your code here

Screenshot, Sketch, or Drawing


Watcha Makin?

If you care to share something about your project, it would be awesome to hear what you're building.

Not sure if it is same as in Table element, or more complex.

Following demo the right-click-selection for Tree element, but with tkinter code.

import PySimpleGUI as sg

def right_click_selection(element, event):
    widget = element.widget
    region = widget.identify('region', event.x, event.y)
    if region in ('cell', 'tree'):
        item = widget.identify_row(event.y)
        key = element.IdToKey[item]
        widget.selection_set([item])            # Only one item selected
        # widget.selection_toggle([item])       # toggole selection of one item
        """
        if isinstance(key, int):                # to get the text of item by the key defined by user when treedata.insert called
            print(data[key][0])
        else:
            k0, k1 = key
            print(data[k0][1][k1])
        """

treedata = sg.TreeData()
data = [
    ['Fruit',     ['Apple', 'Mongo']],
    ['Seasoning', ['Cat', 'Dog', 'Tiger']],
    ['Meat', ['Pork', 'Beef', 'Fish']],
    ['Vegetable', ['Broccoli', 'Cabbages', 'Mushrooms']],
]
for i, first in enumerate(data):
    treedata.Insert('', i, first[0], values=[])         # key defined as integer, i
    for j, second in enumerate(first[1]):
        treedata.Insert(i, (i, j), second, values=[])   # key defined as a 2-tuple, (i, j)

font = ('Courier New', 11)
sg.set_options(font=font)

layout = [
    [sg.Tree(data=treedata, headings=['Unit', 'Unit Price'], show_expanded=True,
     col0_width=15, enable_events=True, visible_column_map=[False,], key='TREE')],
]
window = sg.Window('Title', layout, finalize=True)
tree = window['TREE']
tree.widget.configure(show='tree')  # Hide header
tree.bind('<Button-2>' if sg.running_mac() else '<Button-3>', ' Right Click')   # Bind right button

while True:

    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    elif event == 'TREE Right Click':
        right_click_selection(tree, tree.user_bind_event)

window.close()

Should this be added as an enhancement? What's your opinion Jason?

Not sure why option right_key_selects required for ? Left key selects not work ?
There're lot of different requirements and it's impossible to build everything for people.

commented

@jason990420 , thank you so much for this bit of code. It worked great!
@PySimpleGUI , I believe this should be an enhancement. The reason being that sg.Tree currently allows a right_click_menu. BUT without the right_click_select, the menu is much less useful and predictable. It's counterintuitive to be able to right click on a tree item, pick the menu, and not have it act on the line that was right clicked on. I hope that makes sense.

There're lot of different requirements and it's impossible to build everything for people.

You've got that right!

@rtl19 I do understand what you're saying in how it currently works and can be better.