Search code examples
pythonwxpython

Change mouse cursor over widget


How can I make the mouse cursor change over a particular widget on my wx.Panel?

In the following example, I want to let the cursor change to a hand when it is over bmp2, the image on the upper right, but it stays an arrow. Curious to me is also that if I replace bmp2.SetCursor by bmp4.SetCursor then suddenly the cursor is a hand everywhere.

import wx

class Example(wx.Frame):

    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        self.panel = wx.Panel(self)

        gs = wx.GridSizer(2, 2, 5, 5)

        bmp1 = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.EmptyBitmap(150, 150))
        bmp2 = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.EmptyBitmap(150, 150))
        bmp3 = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.EmptyBitmap(150, 150))
        bmp4 = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.EmptyBitmap(150, 150))

        gs.Add(bmp1, 0, wx.EXPAND)
        gs.Add(bmp2, 0, wx.EXPAND)
        gs.Add(bmp3, 0, wx.EXPAND)
        gs.Add(bmp4, 0, wx.EXPAND)

        bmp2.SetCursor(wx.StockCursor(wx.CURSOR_HAND))

        self.panel.SetSizer(gs) 
        gs.Fit(self)

if __name__ == '__main__':
    app = wx.App()
    Example(None).Show()
    app.MainLoop()

Solution

  • Finally I figured out that I can avoid the problem on my platform (Linux) by wrapping each widget into its own panel.

    Replace the eight lines that create the static bitmaps and add them to the grid sizer by:

        p1 = wx.Panel(self.panel)
        p2 = wx.Panel(self.panel)
        p3 = wx.Panel(self.panel)
        p4 = wx.Panel(self.panel)
        bmp1 = wx.StaticBitmap(p1, wx.ID_ANY, wx.EmptyBitmap(150, 150))
        bmp2 = wx.StaticBitmap(p2, wx.ID_ANY, wx.EmptyBitmap(150, 150))
        bmp3 = wx.StaticBitmap(p3, wx.ID_ANY, wx.EmptyBitmap(150, 150))
        bmp4 = wx.StaticBitmap(p4, wx.ID_ANY, wx.EmptyBitmap(150, 150))
    
        gs.Add(p1, 0, wx.EXPAND)
        gs.Add(p2, 0, wx.EXPAND)
        gs.Add(p3, 0, wx.EXPAND)
        gs.Add(p4, 0, wx.EXPAND)