pythonpython-3.xwxpythonlistctrl

Wxpython ScrollBar


I want that the ScrollBar of the my wx.ListCtrl, will get down automatically when a new item is added to the list and the ScrollBar becomes longer. This is how I create a wx.ListCtrl

import wx

app = wx.App()
frame = wx.Frame(None, -1, title='2', pos=(0, 0), size=(500, 500))
frame.Show(True)
app.SetTopWindow(frame)
message_list = wx.ListCtrl(frame, size=(200, 200), pos=(0, 0),
                       style=wx.LC_REPORT | wx.BORDER_SUNKEN)
message_list.InsertColumn(0, 'Chat: ', width=150)
for i in range(15):
   message_list.InsertItem(i, "name" + str(i))

### I want that after this loop, the scroll bar will be at the end of the list (Name 14)

app.MainLoop()

Solution

  • Select(self, idx, on=1)
    Selects/deselects an item.
    & EnsureVisible(n) makes sure that the selected item is visible i.e. it scrolls the list control.

    So this will work:

    import wx
    
    app = wx.App()
    frame = wx.Frame(None, -1, title='2', pos=(0, 0), size=(500, 500))
    frame.Show(True)
    app.SetTopWindow(frame)
    message_list = wx.ListCtrl(frame, size=(200, 200), pos=(0, 0),
                                style=wx.LC_REPORT | wx.BORDER_SUNKEN)
    message_list.InsertColumn(0, 'Chat: ', width=150)
    for i in range(30):
        message_list.InsertItem(i, "name" + str(i))
    
    msg_endpoint = message_list.GetItemCount() - 1
    message_list.Select(msg_endpoint,1)    #Select last item
    message_list.EnsureVisible(msg_endpoint)
    ### I want that after this loop, the scroll bar will be at the end of the list (Name 14)
    
    app.MainLoop()
    

    Note: Use message_list.Select(i,0) to De-select item(i)
    The scrollbar only becomes visible when required.

    enter image description here