ceccopierangiolieugenio / pyTermTk

Python Terminal Toolkit - a Spiced Up Cross Compatible TUI Library 🌶️
https://ceccopierangiolieugenio.github.io/pyTermTk-Docs/
MIT License
598 stars 23 forks source link

ListBox Get Selected Item #112

Closed nickandwolf closed 10 months ago

nickandwolf commented 1 year ago

I have no clue on how to get a selected item or default the List to specific choice. I've resorted to using:

self.lw.items()[0].selected = True
self.lw.items()[0].highlighted = True

for ensuring the first item is highlighted. And I'm cycling through the list to check what current choice is selected.

For context, the List is used to select objects to edit. I am trying to create a Save function which is checks what step the user is on and then pulls the widgets' current values and assigns them to the right variables.

When trying selectedItems() or selectedLabels() both come up empty ( [] )

nickandwolf commented 1 year ago

That method was too convoluted, I've resorted to storing the callback variable into a global one.

def _listCallback(v=None):
    self.v = v
ceccopierangiolieugenio commented 1 year ago

You can try setCurrentRow as in the demo:

https://github.com/ceccopierangiolieugenio/pyTermTk/blob/6667ee54a8a8c7cc7054406ee6e13c79fbaacc08/demo/demo.py#L249-L251

or setCurrentItem:

https://github.com/ceccopierangiolieugenio/pyTermTk/blob/6667ee54a8a8c7cc7054406ee6e13c79fbaacc08/TermTk/TTkWidgets/listwidget.py#L241-L249

nickandwolf commented 1 year ago

How do I get the currently selected items? Unless there was something pushed since last release and I'm just a big dummy.

ceccopierangiolieugenio commented 1 year ago

ah, sorry you can use: "selectedItems", "selectedLabels"

https://github.com/ceccopierangiolieugenio/pyTermTk/blob/6667ee54a8a8c7cc7054406ee6e13c79fbaacc08/TermTk/TTkWidgets/listwidget.py#L157-L163

as in:

https://github.com/ceccopierangiolieugenio/pyTermTk/blob/6667ee54a8a8c7cc7054406ee6e13c79fbaacc08/demo/showcase/list.py#L61-L62