ListBox (hwx.gui)#

class ListBox(items=[], name='', parent=None, flags=0, **kwds)#

Bases: Widget

A ListBox widget.

Attribute Table#

Name

Type

checked

property

items

property

selectedIndex

property

selectedIndexes

property

selectionMode

property

viewMode

property

Method Table#

Name

Description

append (self, item)

Appends an item to the end of the ListBox.

clear (self)

Removes all items of the List Box.

get (self)

Returns the text of curren row.

insert (self, item, index=9999999)

Inserts an item in the ListBox at position index.

isChecked (self, index)

Returns whether the item at the specified index is checked or not.

remove (self, index)

Remove item at index from List Box.

select (self, index)

Select an index.

set (self, text)

Sets the text in current row.

setChecked (self, index, value=True)

Checks or unchecks the item at the specified index.

sort (self, ascending=True)

Sorts the items in the ListBox, based on their text.

Example

from hwx import gui

values = ['Item-1', 'Item-2', 'Right click in the listbox to add/remove item']

# --------------------------------------------------------------------------
#Context menu for Listbox
def buildContextMenu(menu, index):
if index is None:
   def addItem():
      listBox.append("New-Item")
      listBox.setChecked(len(listBox.items)-1, False)
   menu.insertItem('Add', icon='glyphPlusStrip-16.png', command=addItem)
else:
   def removeItem():
      listBox.remove(index)

   menu.insertItem('Remove', icon='glyphDeleteStrip-16.png', command=removeItem)

# Creating ListBox object with command, so that whenever user selects the
# item in ListBox, it will be printed in output console.
def onItemSelected(event):
print('clicked text:', event.value)
if event.widget.selectedIndex:
   print('selected index:', event.widget.selectedIndex)
else: print('selected indexes:', event.widget.selectedIndexes)

listBox = gui.ListBox(values, command=onItemSelected, onContextMenu=buildContextMenu)
listBox.checked =True
listBox.selectionMode = "MultiSelection"

# Creating Dialog
dialog = gui.Dialog(caption="ListBox", children=listBox, height=145, width=700)

show(dialog)
property items#

Gets/Sets items of the ListBox

property selectedIndex#

(int | None) The currently selected index.

property selectedIndexes#

(list[int] | None) List of currently selected indexes.

select(index)#

Select an index.

Same as setting the selectedIndex except the command callback gets called.

class selectionModeType(value)#

Bases: Enum

An enumeration.

Attribute Table#

Name

Type

ContiguousSelection

selectionModeType

ExtendedSelection

selectionModeType

MultiSelection

selectionModeType

NoSelection

selectionModeType

SingleSelection

selectionModeType

property selectionMode#

Determines the way the items can be selected.

Parameters:

value (ListBox.selectionModeType) – The type of selection to allow.

property checked#

Determines whether items in the list box are checked or not.

By default, all are unchecked.

Returns:

list[bool]

isChecked(index)#

Returns whether the item at the specified index is checked or not.

Parameters:

index (int) –

Returns:

bool

setChecked(index, value=True)#

Checks or unchecks the item at the specified index.

Parameters:
  • index (int) –

  • value (bool) –

get()#

Returns the text of curren row.

set(text)#

Sets the text in current row.

viewModeType#

alias of viewMode

property viewMode#

Determines the way the items can be viewed.

Parameters:

value (ListBox.viewMode) – The type of view to allow.

clear()#

Removes all items of the List Box.

sort(ascending=True)#

Sorts the items in the ListBox, based on their text.

Parameters:

ascending (bool) – If set to True, sort the items in ascending order.

append(item)#

Appends an item to the end of the ListBox.

Parameters:

item (str) – Text value to be inserted.

insert(item, index=9999999)#

Inserts an item in the ListBox at position index.

By default it gets appended to the end.

Parameters:
  • item (str) – Text value to be inserted.

  • index (int) – Index at which item is inserted.

remove(index)#

Remove item at index from List Box.

Parameters:

index (int) – index of item to be removed.