Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Enhancement] Add support for Tree().Update(select_rows=values) #6671

Open
9 tasks done
UpstreamData opened this issue Feb 7, 2024 · 8 comments
Open
9 tasks done
Labels
Done - Install Dev Build (see docs for how) See https://docs.pysimplegui.com/en/latest/documentation/installing_licensing/upgrading/ enhancement New feature or request Port - TK PySimpleGUI

Comments

@UpstreamData
Copy link

UpstreamData commented Feb 7, 2024

Type of Issue - Enhancement


Operating System - Windows 10

PySimpleGUI Port - tkinter


Versions

Python version - 3.12.1

PySimpleGUI Version - 4.60.5

GUI Version - 8.6.13


Your Experience In Months or Years (optional)

Years Python programming experience - 2.5y

Years Programming experience overall - 2.5y

Have used another Python GUI Framework? - 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]

  • Searched main docs for your problem www.PySimpleGUI.org
  • Looked for Demo Programs that are similar to your goal. It is recommend you use the Demo Browser! Demos.PySimpleGUI.org
  • None of your GUI code was generated by an AI algorithm like GPT
  • 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)
  • Run your program outside of your debugger (from a command line)
  • Searched through Issues (open and closed) to see if already reported Issues.PySimpleGUI.org
  • Have upgraded to the latest release of PySimpleGUI on PyPI (lastest official version)
  • Tried using the PySimpleGUI.py file on GitHub. Your problem may have already been fixed but not released

Detailed Description

Right now, Tree().Update has no support for the select_rows parameter. It would be nice to have this for consistency with the Table element, as I am using them semi-interchangeably (I have to use Tree because I am using the -1 column for small image based indicators).

More context:

I am using a set of tabs, each tab having a table of some kind, which I would like to coordinate selections across, since there is so much data that it needs to be split across multiple tabs. All items are already added to each table, and they are updated and sorted as a group, however I now need to have them be able to be selected as a group. For the tables I am using the Update method, but since the Tree object has no select_rows parameter, this doesn't work for it. I have also considered trying to update Tree().SelectedRows and replicate some of the code inside the Tree()._table_clicked() method, but that is pretty janky, and I would rather have first party support directly in the library.

If there is a preference towards not adding this, then an idea of the best way to do this for the Tree element would be great as well.

Code To Duplicate

sg.Tree().Update(select_rows=[])

Screenshot, Sketch, or Drawing


Watcha Makin?

https://github.com/UpstreamData/upstream_config_util

@PySimpleGUI PySimpleGUI added enhancement New feature or request Port - TK PySimpleGUI labels Feb 8, 2024
@PySimpleGUI
Copy link
Owner

Does these issues have any information that may be of help to you? I've not done an exhaustive search. I'm sure Jason will have some magic. In the meantime, thought these may queue up some of the discussion

#1170
#3441

@UpstreamData
Copy link
Author

#1170 was quite the ride, lots of different issues in there for sure, no wonder I didn't find it. The only thing I could find directly relating to this in there was this comment from Jason, suggesting that this may not be something the library wants to provide, but I could be wrong... #1170 (comment)

Regarding #3441, I actually already have a sorting function that sorts the Tree, it wasn't easy to implement per-se, but it wasn't anything crazy.

What I'm asking for is an option to set highlighted (or "selected") rows in the Tree element, the exact functionality provided by the Table element with Update(select_rows=values), located at line 9584 in the current file, whether that be using a list of indices like table, or some other way.

Here's some example images that will hopefully help clarify -

User selects data on first tab:
image

Calling update using the selection event allows me to synchronize the tables:
image

But the final tab uses a Tree for displaying that status of a light, and I'm not sure how to update the selected values:
image

@PySimpleGUI
Copy link
Owner

Like all enhancements, I'll need to get past the PSG 5 launch over the next few days before I can get my bearings and do anything of substance.

If you're in the mood to do a little expansion work on your own, I suggest looking at the _treeview_selected method. It accesses some important data held within the Tree object like the IdtoKey variable. I think there's enough information there for you to be able to construct the data you need to send to self.TKTreeview.selection_set() to select the items you want to select.

Trees and Tables differ in their underlying data structures. Tables are tables and thus have rows. Trees are not the same. They have the secondary TreeData objects with Keys to identify each node in the tree (like a row, but not a row strictly speaking). I likely did a poor job with the terminology by mixing the terms of "row" in the ways I did. It's been a very long time since I looked at it in any detail.

@UpstreamData
Copy link
Author

No problem, even having a starting point is helpful for me, thank you very much! I totally understand that the Tree element is structurally different than the Table, and I also understand that the way I'm using it in this case is not the way it is intended to be used.

If I find something useful, would you like me to send the code here, so you have an idea of what would have to be done to implement this as a feature?

@PySimpleGUI
Copy link
Owner

you have an idea of what would have to be done to implement this as a feature?

Vaguely at best. There's a huge backlog that we're looking at here on top of the huge launch of the new line of products. Just wanting to be honest about not knowing the timing is all.

@UpstreamData
Copy link
Author

you have an idea of what would have to be done to implement this as a feature?

Vaguely at best. There's a huge backlog that we're looking at here on top of the huge launch of the new line of products. Just wanting to be honest about not knowing the timing is all.

Understandable. Solo maintaining a project this large is a monumental task. I'll try to implement it myself, and will keep you updated on how it goes. Appreciate all the work that goes into this library!

@UpstreamData
Copy link
Author

Done. It was trickier than I thought, but still not too bad. Because calling the underlying TTKTreeView functions causes callbacks to be triggered, I have to set an internal flag so that I don't just repeatedly spam updates, but here's the code -

    def update_selected(self, values: list):
        if self.tree_updated:
            self.tree_updated = False
            return
        if self.selected_rows == values:
            values = []
        for table_key in TABLE_KEYS["table"]:
            if table_key == "errors_table":
                continue
            table = window[table_key]
            table.Update(select_rows=values)
        for table_key in TABLE_KEYS["tree"]:
            table = window[table_key]
            table.TKTreeview.selection_set([table.KeyToID[x] for x in values])
            self.tree_updated = True
        self.selected_rows = values

@PySimpleGUI
Copy link
Owner

Very good! It looked like that function had the key member variables that would get you to where you were wanting to go. Glad you've got something working for the time being.

@PySimpleGUI PySimpleGUI added the Done - Install Dev Build (see docs for how) See https://docs.pysimplegui.com/en/latest/documentation/installing_licensing/upgrading/ label Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Done - Install Dev Build (see docs for how) See https://docs.pysimplegui.com/en/latest/documentation/installing_licensing/upgrading/ enhancement New feature or request Port - TK PySimpleGUI
Projects
None yet
Development

No branches or pull requests

2 participants