Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6073491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:16:36+00:00 2026-05-23T10:16:36+00:00

Hi I’m sort new to wxPython and still in the process of learning. I’m

  • 0

Hi I’m sort new to wxPython and still in the process of learning. I’m trying to make a bitmap button using a particular image like this for example: http://i.min.us/idk3Uy.png

The catch is I want to retain the original shape of the image in the button, like a circular button for example instead of a rectangular one (which is default).

I want to know how to do that exactly or if it is possible to do that at all; I did take a look at the documentation and I’ve found the style constant wx.BU_EXACTFIT removes the unnecessary borders…but it’s still not in the desirable shape that I want it to be.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:16:37+00:00Added an answer on May 23, 2026 at 10:16 am

    You’re probably going to have to implement a custom control for this. I’ve done my fair share of custom wxPython controls, so I went ahead and wrote a ShapedButton class for you. =)

    To run this demo, you just need three images:

    • button-normal.png
    • button-pressed.png
    • button-disabled.png

    The three images are used depending on the state of the button. Only the “normal” one is required, but you probably want to at least provide “normal” and “pressed” so the user gets feedback when clicking.

    The control only responds to clicks in non-transparent areas of the normal bitmap. It properly fires the EVT_BUTTON event when clicked and released.

    Enjoy!

    Demo Screenshot

    import wx
    
    class ShapedButton(wx.PyControl):
        def __init__(self, parent, normal, pressed=None, disabled=None):
            super(ShapedButton, self).__init__(parent, -1, style=wx.BORDER_NONE)
            self.normal = normal
            self.pressed = pressed
            self.disabled = disabled
            self.region = wx.RegionFromBitmapColour(normal, wx.Color(0, 0, 0, 0))
            self._clicked = False
            self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
            self.Bind(wx.EVT_SIZE, self.on_size)
            self.Bind(wx.EVT_PAINT, self.on_paint)
            self.Bind(wx.EVT_LEFT_DOWN, self.on_left_down)
            self.Bind(wx.EVT_LEFT_DCLICK, self.on_left_dclick)
            self.Bind(wx.EVT_LEFT_UP, self.on_left_up)
            self.Bind(wx.EVT_MOTION, self.on_motion)
            self.Bind(wx.EVT_LEAVE_WINDOW, self.on_leave_window)
        def DoGetBestSize(self):
            return self.normal.GetSize()
        def Enable(self, *args, **kwargs):
            super(ShapedButton, self).Enable(*args, **kwargs)
            self.Refresh()
        def Disable(self, *args, **kwargs):
            super(ShapedButton, self).Disable(*args, **kwargs)
            self.Refresh()
        def post_event(self):
            event = wx.CommandEvent()
            event.SetEventObject(self)
            event.SetEventType(wx.EVT_BUTTON.typeId)
            wx.PostEvent(self, event)
        def on_size(self, event):
            event.Skip()
            self.Refresh()
        def on_paint(self, event):
            dc = wx.AutoBufferedPaintDC(self)
            dc.SetBackground(wx.Brush(self.GetParent().GetBackgroundColour()))
            dc.Clear()
            bitmap = self.normal
            if self.clicked:
                bitmap = self.pressed or bitmap
            if not self.IsEnabled():
                bitmap = self.disabled or bitmap
            dc.DrawBitmap(bitmap, 0, 0)
        def set_clicked(self, clicked):
            if clicked != self._clicked:
                self._clicked = clicked
                self.Refresh()
        def get_clicked(self):
            return self._clicked
        clicked = property(get_clicked, set_clicked)
        def on_left_down(self, event):
            x, y = event.GetPosition()
            if self.region.Contains(x, y):
                self.clicked = True
        def on_left_dclick(self, event):
            self.on_left_down(event)
        def on_left_up(self, event):
            if self.clicked:
                x, y = event.GetPosition()
                if self.region.Contains(x, y):
                    self.post_event()
            self.clicked = False
        def on_motion(self, event):
            if self.clicked:
                x, y = event.GetPosition()
                if not self.region.Contains(x, y):
                    self.clicked = False
        def on_leave_window(self, event):
            self.clicked = False
    
    def main():
        def on_button(event):
            print 'Button was clicked.'
        app = wx.PySimpleApp()
        frame = wx.Frame(None, -1, 'Shaped Button Demo')
        panel = wx.Panel(frame, -1)
        button = ShapedButton(panel, 
            wx.Bitmap('button-normal.png'), 
            wx.Bitmap('button-pressed.png'), 
            wx.Bitmap('button-disabled.png'))
        button.Bind(wx.EVT_BUTTON, on_button)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.AddStretchSpacer(1)
        sizer.Add(button, 0, wx.ALIGN_CENTER)
        sizer.AddStretchSpacer(1)
        panel.SetSizer(sizer)
        frame.Show()
        app.MainLoop()
    
    if __name__ == '__main__':
        main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.