在MouseOver上的wx.ListCtrl中突出显示ListItem

时间:2011-12-14 22:11:53

标签: python wxpython listctrl

我有一个wxListCtrl,它显示一个包含信息的表(包含行和列字段)。通常,只有在使用鼠标按钮单击时才会突出显示该行。但是我希望没有点击它就有亮点。即,当我将鼠标移动到不同的行时,该行将突出显示而不单击鼠标。这可能吗?

########################################################################

导入wx

import sys,glob

class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "List Control Tutorial")

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)
        self.index = 0

    self.list_ctrl = wx.ListCtrl(panel, size=(-1,100),
                     style=wx.LC_REPORT
                     |wx.BORDER_SUNKEN
                     )
    self.list_ctrl.InsertColumn(0, 'Subject')
    self.list_ctrl.InsertColumn(1, 'Due')
    self.list_ctrl.InsertColumn(2, 'Location', width=125)
    self.list_ctrl.Bind(wx.EVT_ENTER_WINDOW, self.onMouseOver)
    self.list_ctrl.Bind(wx.EVT_LEAVE_WINDOW, self.onMouseLeave)

    btn = wx.Button(panel, label="Add Line")
    btn.Bind(wx.EVT_BUTTON, self.add_line)

    sizer = wx.BoxSizer(wx.VERTICAL)
    sizer.Add(self.list_ctrl, 0, wx.ALL|wx.EXPAND, 5)
    sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
    panel.SetSizer(sizer)   


    bmp = wx.Image("icon.bmp", wx.BITMAP_TYPE_BMP).ConvertToBitmap()
    il = wx.ImageList(16,16)
    il.Add(bmp)
    self.list_ctrl.AssignImageList(il,wx.IMAGE_LIST_SMALL)      
    line = "Line %s" % self.index


    self.list_ctrl.InsertStringItem(self.index, line,-1)  
    self.list_ctrl.SetStringItem(self.index, 1, "01/19/2010")
    self.list_ctrl.SetStringItem(self.index, 2, "USA")
    self.index += 1     

    self.list_ctrl.InsertStringItem(self.index, line,-1)  
    self.list_ctrl.SetStringItem(self.index, 1, "01/19/2010")
    self.list_ctrl.SetStringItem(self.index, 2, "USA")
    #self.list_ctrl.SetItemBackgroundColour(self.index,wx.LIGHT_GREY)
    self.index += 1 

    self.list_ctrl.InsertStringItem(self.index, line,-1)  
    self.list_ctrl.SetStringItem(self.index, 1, "01/19/2010")
    self.list_ctrl.SetStringItem(self.index, 2, "USA")

    self.index += 1             


#----------------------------------------------------------------------
def add_line(self, event):
    if self.index > 0:
        image = 1
    else:
        image = -1    
    line = "Line %s" % self.index
    self.list_ctrl.InsertStringItem(self.index, line,image)
    self.list_ctrl.SetStringItem(self.index, 1, "01/19/2010")
    self.list_ctrl.SetStringItem(self.index, 2, "USA")
    self.index += 1

def onMouseOver(self, event):
    print "mouse over"
    for item in range(self.list_ctrl.GetItemCount()):
         self.list_ctrl.SetItemBackgroundColour(item,wx.NullColor)
    x = event.GetX()
    y = event.GetY()
    item, flags = self.list_ctrl.HitTest((x, y))
    self.list_ctrl.SetItemBackgroundColour(item,wx.RED)
    #self.list_ctrl.RefreshItems(0,2)
    event.Skip()

def onMouseLeave(self, event):
    print "mouse leave"
    for item in range(self.list_ctrl.GetItemCount()):
         self.list_ctrl.SetItemBackgroundColour(item,wx.NullColor)
    #self.list_ctrl.RefreshItems(0,2)
    event.Skip()
'''         
def onMouseOver(self, event):    #USED to display tooltip on items that cannot be selected

    x = event.GetX()
    y = event.GetY()
    item, flags = self.list_ctrl.HitTest((x, y))
    color = self.list_ctrl.GetItemBackgroundColour(item) 
    if color == wx.NullColor:

        self.list_ctrl.SetItemBackgroundColour(item,wx.RED)
    elif color == wx.RED:
        item = item - 1
        color = self.list_ctrl.GetItemBackgroundColour(item) 
        self.list_ctrl.SetItemBackgroundColour(item,wx.Nu)

'''       
#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()    

3 个答案:

答案 0 :(得分:2)

在创建ListCtrl并将其放入变量时,我会尝试抓取其中一个ListItem的背景颜色:

self.defaultItemColor = someListItem.GetBackgroundColour()

然后用它来改变颜色。在调用item的setter之后,有时需要调用ListCtrl的SetItem(listItem)方法。出于某种原因,将背景设置为NullColour不适用于ListCtrl。当我为我的一个应用程序创建一个黑暗模式时,我发现了这一点。我实际上是在这里写的:http://www.blog.pythonlibrary.org/2011/11/05/wxpython-creating-a-dark-mode/

答案 1 :(得分:0)

这不是“轻松”开箱即用的,但你应该能够做一些修补。

您必须向wxListCtrl对象添加mouseover和mouseout事件侦听器,然后在每次获得mouseover事件时测试正在命中哪个项目。您可以缓存列表项的坐标,但是如果您沿着此路线滚动列表和窗口大小调整可能会出现问题。

一些代码可以帮助您入门(未经过测试,可能无效,您需要将MyListCtrl添加到合适的wx.Frame中):

import wx

class MyListCtrl(wx.ListCtrl):
    def __init__(self, parent, id):
        wx.ListCtrl.__init__(self, parent, id)
        self.Bind(wx.EVT_ENTER_WINDOW, self.onMouseOver)
        self.Bind(wx.EVT_LEAVE_WINDOW, self.onMouseLeave)

    def onMouseOver(self, event):
        #Loop through all items and set bgcolor to default, then:
        item = self.HitTest(event.GetPosition())
        self.SetItemBackgroundColour(item, 'Green')
        self.RefreshItems()
        event.Skip()

    def onMouseLeave(self, event):
        #Loop through all items and set bgcolor to default, then:
        self.RefreshItems()
        event.Skip()

答案 2 :(得分:0)

我知道这是一个迟到的回复,但以下对我有用:         self.listCtrl.Bind(wx.EVT_MOTION,self.onMouseOver) 并将self.previous_item设置为-1

我用它来用鼠标突出显示行并同时改变工具提示。

def onMouseOver(self, event):
    x = event.GetX()
    y = event.GetY()
    self.item, flags = self.listCtrl.HitTest((x, y))
    if self.item < 0:
        self.listCtrl.SetToolTipString("Colour codes Red - Loaded, Yellow - In Progress, Green - Finished, Blue - Invoiced, White - User defined")
        return
    if self.item != self.previous_item:
        self.old_item = self.previous_item
        self.previous_item = self.item
    else:
        return
    bg_colour = self.listCtrl.GetItemBackgroundColour(self.item) 
    if bg_colour == wx.BLACK or bg_colour == wx.NullColour:
        self.listCtrl.SetItemBackgroundColour(self.item,"#3246A8")
        self.listCtrl.SetItemBackgroundColour(self.old_item,wx.BLACK)
    elif bg_colour == "#3246A8":
        self.listCtrl.SetItemBackgroundColour(self.item,wx.BLACK)
    self.currentItem = self.item
    rowid = self.listCtrl.GetItem(self.currentItem,13)
    stat_test = rowid.GetText()
    rowid = self.listCtrl.GetItem(self.currentItem,1)
    file_test = rowid.GetText()
    rowid = self.listCtrl.GetItem(self.currentItem,4)
    running_test = rowid.GetText()

    if stat_test == "0":
        self.listCtrl.SetToolTipString("File currently playing\nRunning time "+running_test)
    elif stat_test == "1":
        self.listCtrl.SetToolTipString("In Progress\nRunning time "+running_test)
    elif stat_test == "2":
        self.listCtrl.SetToolTipString("Finished\nRunning time "+running_test)
    elif stat_test == "3":
        self.listCtrl.SetToolTipString("Invoiced\nRunning time "+running_test)
    if file_test == self.file_playing and stat_test == "1":
        self.listCtrl.SetToolTipString("File currently playing & In Progress\nRunning time "+running_test)
    if file_test == self.file_playing and stat_test == "2":
        self.listCtrl.SetToolTipString("File currently playing but Finished\nRunning time "+running_test)
    if file_test == self.file_playing and stat_test == "3":
        self.listCtrl.SetToolTipString("File currently playing but Invoiced\nRunning time "+running_test)

我希望它可以帮到某人