Skip to content
Related Articles

Related Articles

Improve Article

wxPython – Change Cursor on hover on Button

  • Last Updated : 18 Jun, 2020

In this article we will learn that how can we change crsor when it hovers on Button present in frame. We need to follow some steps as followed.

Step 1- Create a wx.Image object with image you want to use as cursor image.
Step 2- Create a wx.Cursor object and pass wx.Image object above created.
Step 3- Set the cusrsor using SetCursor() function.

Syntax: wx.Button.SetCursor(cursor)

Parameters:

ParameterInput TypeDescription
cursorwx.Cursorcursor to be set.

Code Example:






import wx
  
  
class Example(wx.Frame):
  
    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs)
        self.InitUI()
  
    def InitUI(self):
        self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
        self.pnl = wx.Panel(self)
  
  
        # CREATE BUTTON AT POINT (20, 20)
        self.st = wx.Button(self.pnl, id = 1, label ="Button", pos =(20, 20),
                                size = wx.DefaultSize,  name ="button")
        # CREATE CURSOR OBJECT
        c = wx.Cursor(wx.Image('pointer.png'))
        # SET c AS CURSOR
        self.st.SetCursor(c)
        self.SetSize((350, 250))
        self.SetTitle('wx.Button')
        self.Centre()
  
def main():
    app = wx.App()
    ex = Example(None)
    ex.Show()
    app.MainLoop()
  
  
if __name__ == '__main__':
    main()

Output Window:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :