How to make a wxpython Bitmap Button with only image and no extra pixels and border around image

StackOverflow https://stackoverflow.com/questions/21645136

  •  08-10-2022
  •  | 
  •  

Question

I am trying to use an image as a button in wxpython. What I want is that there should only be a bitmap and no other border or any extra pixel around the button. I would really only want to capture a click on a bitmap, so maybe I do not need an actual button. "Pressing down" the button is hence not required.

One option that I am using a PlateButton with platebutton.PB_STYLE_NOBG which works fine only when it is displaying the image without any mouse hover or clicks. Now when I hover the button, what I want is a shadowed image of same image(only image and no border or anything), but what I get is a square border around my image.

My code :

import wx.lib.platebtn as platebutton


imageButton = platebutton.PlateButton(self._ribbon,wx.ID_NEW, bmp = wx.Bitmap("image.png", wx.BITMAP_TYPE_ANY), pos = (0,0), size = (37,17), style= platebutton.PB_STYLE_DEFAULT | platebutton.PB_STYLE_NOBG)
Was it helpful?

Solution

You can directly use static bitmap and capture the mouse event in this bitmap.

try this:

     sBitMap = wx.StaticBitmap(self._ribbon, -1, wx.Bitmap("image.png", wx.BITMAP_TYPE_ANY), (0, 0), (37,17))
     sBitMap.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)

def OnLeftDown(self, e):
    print "OnLeftDown"

Edit:

Fetch the size from the source image and use it to create the static bitmap

sBitMap = wx.StaticBitmap(self._ribbon, -1, wx.Bitmap("image.png", wx.BITMAP_TYPE_ANY), (0, 0), (bmp.GetWidth(), bmp.GetHeight()))

OTHER TIPS

I'm using the GenBitmapButton from wx.lib.buttons

path="path to bitmap"
size=(size of bitmap)
b=GenBitmapButton(Parent,wx.ID_ANY, bitmap=wx.Bitmap(path),
                  style=wx.NO_BORDER|wx.BU_EXACTFIT,size=size)

This is doing the trick just fine and I have bitmaps lined up with no spacing between them for buttons.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top