Search code examples
pythonwxpythonwxwidgets

wxPython: how to correcly select ListCtrl item?


I have python 2.7 with wxWidgets 2.8.11.0 installed on my Windows 8. If i execute the following code:

import wx

app = wx.App( redirect = False )
wnd = wx.Frame( parent = None )
widget = wx.ListCtrl( parent = wnd, style = wx.LC_REPORT )
widget.InsertColumn( 0, "items" )
widget.InsertStringItem( 0, "foo" )
widget.InsertStringItem( 1, "bar" )
widget.InsertStringItem( 2, "baz" )
widget.Select( 1 )
wnd.Show()
app.MainLoop()

I'm shown a window with list of 3 items, second selected. But if i press "down" key - first item is selected! Is it possible to select item so pressing "up" and "down" key will move existing selection and will not jump to first item?


Solution

  • Use both Select (to highlight) and Focus (to make the line the current line) together:

    ........
    widget.Focus(1)
    widget.Select(1)
    ..........