简体   繁体   English

如何使wxpython位图按钮只有图像,没有额外的像素和图像边框

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

I am trying to use an image as a button in wxpython. 我想在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. 我正在使用带有platebutton.PB_STYLE_NOBG的PlateButton的一个选项,只有在没有任何鼠标悬停或点击的情况下显示图像时才能正常工作。 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)

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()))

I'm using the GenBitmapButton from wx.lib.buttons 我正在使用wx.lib.buttons的GenBitmapButton

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. 这样做很好,我有位图,按钮之间没有间距。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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