pythonwxpython

Is it possible to bind an on click event on a wx.StaticText?


I have this code:

import wx

class Plugin(wx.Panel):
    def __init__(self, parent, *args, **kwargs):
        panel = wx.Panel.__init__(self, parent, *args, **kwargs)
        self.colorOver = ((89,89,89))
        self.colorLeave = ((110,110,110))
        self.colorFont = ((131,131,131))
        self.SetBackgroundColour(self.colorLeave)
        self.SetForegroundColour(self.colorLeave)
        self.name = "Plugin"
        self.overPanel = 0
        self.overLabel = 0

        sizer = wx.BoxSizer(wx.VERTICAL)
        name = wx.StaticText(self, -1, ' ' + self.getName())
        close = wx.StaticText(self, -1, ' X ')

        gs = wx.GridSizer(2, 2, 0, 0)
        gs.AddMany([(name, 0, wx.ALIGN_LEFT), (close, 0, wx.ALIGN_RIGHT)])

        sizer.Add(gs, 1, wx.EXPAND)
        self.SetSizer(sizer)

        .... ....

Is it possible to left click on the StaticText close and hide the panel itself?


Solution

  • I dont know if it is possible to bind a wx.EVT_LEFT_DOWN to a StaticText widget. You could use a button to call self.Hide(). Maybe a BitmapButton if you want a custom look.

    class myPanel(wx.Panel):
        def __init__(self, parent, *args, **kwargs):
            wx.Panel.__init__(self, parent, *args, **kwargs)
            bitmap = wx.EmptyBitmap(15,15)
            self.button = wx.BitmapButton(self, -1, bitmap=bitmap, size=(15,15), style=wx.NO_BORDER)
            self.Bind(wx.EVT_BUTTON, self.onClick, self.button)
    
    def onClick(self, event):
        self.Hide()