0

我有一个 wxListCtrl,它显示一个包含信息的表格(带有行和列字段)。通常,只有当您使用鼠标按钮单击该行时,该行才会突出显示。但我想在不点击的情况下获得亮点。即当我将鼠标移动到不同的行时,该行将被突出显示而无需单击鼠标。这可能吗?

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

导入 wx

导入系统,全局

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()    
4

3 回答 3

2

当您创建 ListCtrl 并将其放入变量时,我会尝试获取其中一个 ListItems 的背景颜色:

self.defaultItemColor = someListItem.GetBackgroundColour()

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

于 2011-12-15T13:58:46.560 回答
1

我知道这是一个较晚的响应,但以下对我有用: 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)

我希望它可以帮助某人

于 2015-04-09T10:05:35.170 回答
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()
于 2011-12-14T23:44:44.103 回答