0

我正在尝试使用 Mike Driscoll 的解决方案在 wxPython 网格中制作工具提示消息,如下所述:http: //www.blog.pythonlibrary.org/2010/04/04/wxpython-grid-tips-and-tricks/。这是我的最小示例。

import wx
import wx.grid

class GridFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY)
        self.panel = wx.Panel(self)
        self.InitUI()

    def InitUI(self):
        hbox = wx.BoxSizer(wx.HORIZONTAL)
        self.grid = wx.grid.Grid(self, -1)
        self.grid.ClearGrid()
        self.grid.CreateGrid(10, 5)
        self.grid.AutoSize()
        self.grid.GetGridWindow().Bind(wx.EVT_MOTION, lambda event: self.onMouseOver(event, self.grid))
        hbox.Add(self.grid, flag=wx.ALL, border=10)
        self.panel.SetSizer(hbox)
        hbox.Fit(self)
        self.Centre()
        self.Show()

    def onMouseOver(self, event, grid):
        """                                                                                                                                        
        Displays a tooltip over any cell in a certain column                                                                                       
        """
        x, y = grid.CalcUnscrolledPosition(event.GetX(),event.GetY())
        coords = grid.XYToCell(x, y)
        col = coords[1]
        row = coords[0]

        if col == 1:
            msg = "This is Row %s, Column %s!" % (row, col)
            print msg
            event.GetEventObject().SetToolTipString(msg)
        else:
            event.GetEventObject().SetToolTipString('')


if __name__ == "__main__":
    app = wx.PySimpleApp(redirect=False)
    app.frame = GridFrame()
    app.frame.Show()
    app.frame.Center()
    app.MainLoop()

工具提示根本不显示。打印语句按预期工作,所以我知道绑定有效并且事件被捕获。我假设我错过了一些简单的东西 - 也许我需要在某处初始化工具提示?- 但我不确定是什么。有任何想法吗?

更新:

问题似乎是 wxPython 的版本和平台的组合。该代码在带有 wxPython 2.8.10.1 的 Windows 上按预期工作。它在 Mac 上使用 2.9.2.4 失败,但在 3.0.2.0 上成功。一般来说,我们需要 GUI 的用户安装 Enthought 的 Python 的 Canopy 发行版,它附带 2.9.2.4(wxPython 的失败版本)。

使用 event.Skip() 似乎并不重要。

既然我知道这是一个 wxPython 版本问题,我会以不同的方式处理这个问题。

4

1 回答 1

1

尝试使用

def onMouseOver(self, event, grid):
    """
    Displays a tooltip over any cell in a certain column
    """
    x, y = grid.CalcUnscrolledPosition(event.GetX(),event.GetY())
    coords = grid.XYToCell(x, y)
    col = coords[1]
    row = coords[0]

    if col == 1:
        msg = "This is Row %s, Column %s!" % (row, col)
        print msg
        self.grid.GetGridWindow().SetToolTipString(msg)
    else:
        self.grid.GetGridWindow().SetToolTipString('')
    #wx.Window.ToolTip()
    #wx.Window.Tool
    event.Skip()

我不确定你为什么要传递网格,因为它可以通过 self 属性获得

于 2014-12-30T19:18:37.640 回答