wxpython listctrl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shuf
    New Member
    • Mar 2007
    • 8

    wxpython listctrl

    Hello,

    I was wondering if anyone knows how to change the color of a selected item in a listctrl (for example, on my system a selected item is highlighted in blue). I could not find any information on this, other than it is a system defined parameter. Any help is greatly appreciated.

    Thanks in advance,
    Stephen
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by shuf
    Hello,

    I was wondering if anyone knows how to change the color of a selected item in a listctrl (for example, on my system a selected item is highlighted in blue). I could not find any information on this, other than it is a system defined parameter. Any help is greatly appreciated.

    Thanks in advance,
    Stephen
    In your EVT_LIST_ITEM_R IGHT_CLICK handler:
    Code:
    item = event.GetItem()
    SetItemBackgroundColour(item, colorTuple)

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by bartonc
      In your EVT_LIST_ITEM_R IGHT_CLICK handler:
      Code:
      item = event.GetItem()
      SetItemBackgroundColour(item, colorTuple)
      Of course, you'll probably want to keep the old value returned by
      Code:
      GetItemBackgroundColour()
      around and set it back when you get an EVT_LIST_ITEM_D ESELECTED event.

      Comment

      • shuf
        New Member
        • Mar 2007
        • 8

        #4
        Originally posted by bartonc
        In your EVT_LIST_ITEM_R IGHT_CLICK handler:
        Code:
        item = event.GetItem()
        SetItemBackgroundColour(item, colorTuple)
        I wanted to do the above on the EVT_LIST_ITEM_S ELECTED event, so I assume that it will not be different. I bound the event to the following method:
        Code:
        def OnSelected(self, event):
            item = event.GetItem()
            self.listctrl.SetItemBackgroundColour(item, wx.RED)
        But, I get an error that a number is expected for argument number 2 (item). So if I change the code to:
        Code:
        def OnSelected(self, event):
            item = self.listctrl.GetFirstSelected()
            self.listctrl.SetItemBackgroundColour(item, wx.RED)
        It changes the color under the selection highlighting (I can only see the color if I deselect the item). Is there a way to change the selection highlighting color? Thanks again for your response.
        Last edited by bartonc; Nov 5 '07, 03:45 PM.

        Comment

        • bartonc
          Recognized Expert Expert
          • Sep 2006
          • 6478

          #5
          Originally posted by shuf
          I wanted to do the above on the EVT_LIST_ITEM_S ELECTED event, so I assume that it will not be different. I bound the event to the following method:

          def OnSelected(self , event):
          item = event.GetItem()
          self.listctrl.S etItemBackgroun dColour(item, wx.RED)

          But, I get an error that a number is expected for argument number 2 (item). So if I change the code to:

          def OnSelected(self , event):
          item = self.listctrl.G etFirstSelected ()
          self.listctrl.S etItemBackgroun dColour(item, wx.RED)

          It changes the color under the selection highlighting (I can only see the color if I deselect the item). Is there a way to change the selection highlighting color? Thanks again for your response.
          Yeah, I don't see anything about setting the selected color to a different color from the default either.

          Comment

          • shuf
            New Member
            • Mar 2007
            • 8

            #6
            Originally posted by bartonc
            Yeah, I don't see anything about setting the selected color to a different color from the default either.
            Thanks for your help.

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by shuf
              Thanks for your help.
              a wx.Grid has what you are looking for:
              Code:
                      self.grid1.SetSelectionBackground(wx.Colour(115, 247, 121))

              Comment

              • CraigDouglas
                New Member
                • Nov 2007
                • 1

                #8
                I'm fully aware this is in old thread, but I cam across exactly the same problem and this was the only directly relevent page that came up so I thought i'd post the solution here for anyone else that has the same issue in future.

                You need to bind an event handler on EVT_LIST_ITEM_S ELECTED then change the item's state flags in the handler like so:

                Code:
                ls = wx.ListCtrl
                ls.Bind(wx.EVT_LIST_ITEM_SELECTED, _item_selected)
                
                def _item_selected(evt):
                    i = evt.GetIndex() # Find item selected
                    ls.SetItemState(i, wx.LIST_STATE_FOCUSED, wx.LIST_STATE_SELECTED) # Clear selected flag and just leave focused flag so that a box is drawn round selected item
                    evt.Skip() # Let the event propogate

                Comment

                • bartonc
                  Recognized Expert Expert
                  • Sep 2006
                  • 6478

                  #9
                  Originally posted by CraigDouglas
                  I'm fully aware this is in old thread, but I cam across exactly the same problem and this was the only directly relevent page that came up so I thought i'd post the solution here for anyone else that has the same issue in future.

                  You need to bind an event handler on EVT_LIST_ITEM_S ELECTED then change the item's state flags in the handler like so:

                  Code:
                  ls = wx.ListCtrl
                  ls.Bind(wx.EVT_LIST_ITEM_SELECTED, _item_selected)
                  
                  def _item_selected(evt):
                      i = evt.GetIndex() # Find item selected
                      ls.SetItemState(i, wx.LIST_STATE_FOCUSED, wx.LIST_STATE_SELECTED) # Clear selected flag and just leave focused flag so that a box is drawn round selected item
                      evt.Skip() # Let the event propogate
                  Thank you very much. It's always appreciated when we get this kind of contribution.

                  Comment

                  Working...