在 MouseOver 上突出显示 wx.ListCtrl 中的 ListItem
我有一个 wxListCtrl 显示带有信息的表格(带有行和列字段)。通常,仅当您用鼠标按钮单击该行时,该行才会突出显示。但我想突出显示而不点击它。即当我将鼠标移动到不同的行时,该行将突出显示而无需单击鼠标。这可能吗?
########################################################################
导入wx
导入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()
I have a wxListCtrl that display a table with information (with rows and column fields). Typically the row will be highlighted only when you click it with the mouse button. But I would like to have the highlight without clicking it. i.e. When I move the mouse to different row, the row will be highlighted without clicking the mouse. Is this possible?
########################################################################
import 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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您创建 ListCtrl 并将其放入变量中时,我会尝试抓取其中一个 ListItems 的背景颜色:
然后使用它来将颜色改回来。调用项目的 setter 后,有时还需要调用 ListCtrl 的 SetItem(listItem) 方法。由于某些原因,将背景设置为 NullColour 不适用于 ListCtrl。当我为我的一个应用程序创建黑暗模式时,我发现了这一点。我实际上在这里写过: http:// /www.blog.pythonlibrary.org/2011/11/05/wxpython-creating-a-dark-mode/
I would try grabbing the background color of one of the ListItems when you create the ListCtrl and putting it in a variable:
Then use that to change the color back. After you call the item's setter, you sometimes need to call the ListCtrl's SetItem(listItem) method as well. For some reason, setting the background to NullColour doesn't work with the ListCtrl. I discovered that back when I was creating a dark mode for one of my applications. I actually wrote about it here: http://www.blog.pythonlibrary.org/2011/11/05/wxpython-creating-a-dark-mode/
我知道这是一个迟到的回复,但以下内容对我有用:
self.listCtrl.Bind(wx.EVT_MOTION, self.onMouseOver)
并将 self.previous_item 最初设置为 -1
我用它来突出显示鼠标悬停在该行上的行,并同时更改工具提示。
我希望它能帮助别人
I know that this is a late response but the following works for me using:
self.listCtrl.Bind(wx.EVT_MOTION, self.onMouseOver)
and setting self.previous_item initially to -1
I am using it to highlight the row with the mouse over it and alter the tooltip at the same time.
I Hope it helps someone
这并不是“容易”开箱即用的,但您应该能够通过一些修改来完成。
您必须向 wxListCtrl 对象添加 mouseover 和 mouseout 事件侦听器,然后在每次收到 mouseover 事件时测试哪个项目被击中。您也许能够缓存列表项的坐标,但如果您采用此方法,滚动列表和窗口大小调整可能会造成问题。
一些帮助您入门的代码(未经测试,可能不起作用,您需要将 MyListCtrl 添加到合适的 wx.Frame ):
This is not "easily" done out of the box, but you should be able to do it with a bit of tinkering.
You'll have to add a mouseover and mouseout event listener to your wxListCtrl object, and then test which item is being hit each time you get a mouseover event. You may be able to cache the coordinates of the list items, but scrolling lists and window resizes could pose a problem if you go this route.
Some code to get you started (not tested, probably won't work and you'd need to add a MyListCtrl to a suitable wx.Frame):