wx.EVT_ENTER_WINDOW generates another effection

Hi
I am trying to have a effection that when the mouse enters the button it become red and set its label,and when the mouse leaves the button it become yellow and reset its label.But setting label can work ,setting color can’t run .It only become red when I click it .But I don’t Bind click event. why can it become red when the mouse enters it?

import wx

class MouseEventFrame(wx.Frame):
    
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, 'Frame With Button', 
                size=(300, 100))
        self.panel = wx.Panel(self)                              
        self.button = wx.Button(self.panel, label="Not Over", pos=(100, 15))       
        self.button.Bind(wx.EVT_ENTER_WINDOW, self.OnEnterWindow) 
        self.button.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindow)         
 
    def OnEnterWindow(self, event):        
        self.button.SetBackgroundColour('red')
        self.button.SetLabel("Over Me!")
   
        event.Skip()
        
    def OnLeaveWindow(self, event):
        self.button.SetLabel("Not Over")
        self.button.SetBackgroundColour('yellow')
        event.Skip()        
    
if __name__ == '__main__':
    app = wx.App()
    frame = MouseEventFrame(parent=None, id=-1)
    frame.Show()
    app.MainLoop()

Is this on Windows?
See here Can't make it work wx.Button.SetBackgroundColour on wx.EVT_ENTER_WINDOW - #6 by DietmarSchwertberger

Regards,
Dietmar

Hi,Dietmar.
I‘v read all the documents you give and runned all the programs mentioned in these documents.The problem has been there.The button event doesn’t response perfectly。When the mouse enter it,it can’t change the color.It only changes color when the mouse leaves it.

Please describe the problem. What does it do on entering? Does it color the button in light blue?

And again: is this on Windows?

well, do you need a frame around your button (frameless is en vogue) :blonde_woman:

My machine is windows10, Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32,wxpython for 4.1.1.

I want to set two events on the button.one is wx.EVT_ENTER_WINDOW event,the other is wx.EVT_LEAVE_WINDOW event.When the mouse enters the button ,the button can change its backgroundcolor and the mouse leaves the botton ,the button will change another backgroundcolor. for exampl: the button’s bcackground turns green when the leaving event occurs and red when the entry event occurs.The question is when the entry event occurs,the button’s backgroundcolor doesn’t turns red.

import wx

class MouseEventFrame(wx.Frame):

def __init__(self, parent, id):
    wx.Frame.__init__(self, parent, id, 'Frame With Button', 
            size=(300, 100))
    self.panel = wx.Panel(self)                              
    self.button = wx.Button(self.panel, label="Not Over", pos=(100, 15))       
    self.button.Bind(wx.EVT_ENTER_WINDOW, self.OnEnterWindow) 
    self.button.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveWindow)         

def OnEnterWindow(self, event):        
    self.button.SetBackgroundColour('red')
    self.button.SetLabel("Over Me!")

    event.Skip()
    
def OnLeaveWindow(self, event):
    self.button.SetLabel("Not Over")
    self.button.SetBackgroundColour('Green')
    event.Skip()        


if  __name__ == ‘__main__’:
  app = wx.App()
  frame = MouseEventFrame(parent=None, id=-1)
  frame.Show()
  app.MainLoop()

Once more: Please describe the problem. What does it do on entering? Does it color the button in light blue?

what about something like this

import wx

class Gui(wx.Frame):
    def __init__(self, parent):
        super().__init__(parent, title='enter / leave button')

        btn = wx.Button(self, style=wx.BORDER_NONE)

        def evt_enter(_):
            btn.SetBackgroundColour(wx.RED)
            btn.SetLabelMarkup(''.join(('<b>', 'Over Me', '</b>')))
        btn.Bind(wx.EVT_ENTER_WINDOW, evt_enter)
        def evt_leave(_):
            btn.SetBackgroundColour(wx.YELLOW)
            btn.SetLabelMarkup(''.join(('<b><i>', 'Not Over', '</i></b>')))
        btn.Bind(wx.EVT_LEAVE_WINDOW, evt_leave)
        def evt_button(_):
            btn.SetBackgroundColour(None)
            btn.SetLabel("yes, I'm a button..")
        btn.Bind(wx.EVT_BUTTON, evt_button)

        self.Show()

app = wx.App()
Gui(None)
app.MainLoop()
1 Like

This is what I want.But I don’t understand why your program can run in this way and my program can’t? Can you tell me the principle?Thanks.

Excellent!

The difference is style=wx.BORDER_NONE but I don’t know why.

1 Like

With wx.BORDER_NONE, wx is using style BS_FLAT which seems to avoid the default shading (probably the OP was seeing a blue button, as described in the other thread, but the OP did not want to describe or confirm this).

1 Like

Thanks ! :+1: :+1: :+1: :+1: :+1: :+1: :+1: :+1: