简体   繁体   中英

How to disable the mouse over color change in wxPython

I have a custom button and want to disable the bright highlight color on mouse hover. I have tried to call event.Skip() in the EVT_ENTER_WINDOW , but the highlight color still shows up.

    class CustomButton(Button):
        def __init__(self, parent, id, label, style):
            Button.__init__(self, parent, id=id, label=label, style=style)
    
            self.Bind(EVT_ENTER_WINDOW, self.OnEnterWindow)
    
        def OnEnterWindow(self, event):
            event.Skip()

One option is to create your own custom event , then activate that event and perform whatever you need to do with it ie in your case, flip the colour of a button.

Custom events make use of wx.lib.newevent eg

import wx
import wx.lib.newevent

NewEvent, EVT_MY_EVENT = wx.lib.newevent.NewEvent()
CMD_ID = wx.NewIdRef()

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame()
        return True

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="Window", pos=(100,150), size=(250,200))
        sizer = wx.BoxSizer()
        self.button1 = wx.Button(self, CMD_ID, label="Button 1")
        sizer.Add(self.button1)
        self.Bind(wx.EVT_BUTTON, self.OnButton, id=CMD_ID)
        self.Bind(EVT_MY_EVENT, self.OnMyEvent)
        self.Layout()
        self.Show()

    def OnButton(self, event):
        id = event.GetId()
        event = NewEvent(action="perform a defined action",button=id,other_setting=1234)
        wx.PostEvent(self, event)

    def OnMyEvent(self, event):
        button = event.button
        action = event.action
        other = event.other_setting
        print("event button number", button)
        print("event action request", action)
        print("event other", other)

if __name__ == "__main__":
    app = MyApp()
    app.MainLoop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM