FindString() method for wx ListBox not working for me

44 Views Asked by At

This is a part of the code which successfully displays the values in the dict art in a wxListBox. However, nothing I try returns other than -1 from the last line below. Can anyone see why it doesn't work?

    art = {'A1':'Horace Silver', 'A2':'Murray Perahia', 'A3':'Kieth Jarret',
           'A4':'Andras Schiff', 'A5':'Tord Gustavsen Trio'}
    art = dict(sorted(art.items(), key=lambda item: item[1]))
    self.list_box_1.InsertItems(list(art.values()),0)
    print(self.list_box_1.FindString('ray', True))

I have tried with and without case sensitivity, single and double quotes around the string, whole words and parts. No error messages are sent.

I gave up looking for the error and discovered that this function works:-

def checkname(self,l,n):
    for i in range(l.GetCount()):
        name = l.GetString(i)
        x = name.find(n,0)
        if x > -1:
            return [i, name]

l is the name of a listbox and n is the string to find. It is limited to looking from the beginning.

1

There are 1 best solutions below

0
Rolf of Saxony On

As the FindString function is looking for a match on the label, you will need to manually find partial matches.

One way to achieve that is to use the wx.ListBox GetStrings() function and then query that.

Something like this: (here I query the labels for "ra" and then set each item found as Selected)

import wx

class TestListBox(wx.Frame):
    def __init__(self, *args, **kwds):
        wx.Frame.__init__(self, *args, **kwds)
        art = {'A1':'Horace Silver', 'A2':'Murray Perahia', 'A3':'Kieth Jarret',
           'A4':'Andras Schiff', 'A5':'Tord Gustavsen Trio'}
        art = dict(sorted(art.items(), key=lambda item: item[1]))
        self.lb = wx.ListBox(self, wx.NewIdRef(), choices=[], style=wx.LB_EXTENDED|wx.LB_MULTIPLE)
        self.lb.InsertItems(list(art.values()),0)
        self.lb.Append('Roger the Dodger')
        self.lb.Insert('RolfofSaxony', 1)
        labels = self.lb.GetStrings()
        indicies = self.find_string(labels, 'ra', True)
        print("Matches:", indicies)
        for item in indicies:
            self.lb.SetSelection(item)
        self.Show()

    def find_string(self, search_list, search_str, caseSensitive=False):
        if not caseSensitive:
            lowercase_list = [item.lower() for item in search_list]
            search_list = lowercase_list
            search_str = search_str.lower()
        return [index for (index, item) in enumerate(search_list) if search_str in item]

if __name__ == '__main__':
    app = wx.App()
    TestListBox(None)
    app.MainLoop()

enter image description here