在wx.ListCtrl在Mous上

2024-09-28 05:22:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个wxListCtrl,它显示一个包含信息的表(包含行和列字段)。通常,只有当您用鼠标键单击该行时,该行才会高亮显示。但我想不点击高亮显示。i、 当我将鼠标移到不同的行时,该行将被突出显示而不单击鼠标。这可能吗?在

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

导入wx

导入系统,全局

^{pr2}$

Tags: 信息系统鼠标全局行和列wx行时pr2
3条回答

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

self.defaultItemColor = someListItem.GetBackgroundColour()

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

我知道这是一个迟来的答复,但我使用以下方法: self.listCtrl.Bind(wx.EVT_运动, self.onMouseOver公司) 和设置自我上一项最初为-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)

我希望它能帮助别人

这并不是“容易”就可以实现的,但是您应该能够通过一些修补来完成它。在

您必须将mouseover和mouseout事件侦听器添加到wxListCtrl对象中,然后在每次获取mouseover事件时测试所命中的项。您可能可以缓存列表项的坐标,但如果您按此方法滚动列表和调整窗口大小可能会带来问题。在

一些让你开始的代码(没有经过测试,可能行不通,你需要在合适的wx.框架)公司名称:

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

相关问题 更多 >

    热门问题