In this Python GUI article i want to show you creating Bitmap Button in wxPython. A bitmap button is a control that contains a bitmap.
So This class supports the following styles:
wx.BU_LEFT
: Left-justifies the bitmap label.wx.BU_TOP
: Aligns the bitmap label to the top of the button.wx.BU_RIGHT
: Right-justifies the bitmap label.wx.BU_BOTTOM
: Aligns the bitmap label to the bottom of the button.
Also you can check More GUI Development Tutorials in the below link.
1: PyQt5 GUI Development Tutorials
2: TKinter GUI Development Tutorials
3: Pyside2 GUI Development Tutorials
4: Kivy GUI Development Tutorials
This is complete code for Python GUI Bitmap Button in wxPython
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
import wx class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title, size = (600,400)) self.panel = MyPanel(self) class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) vbox = wx.BoxSizer(wx.VERTICAL) hbox = wx.BoxSizer(wx.HORIZONTAL) imageFile = "copy.png" image1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap() self.button1 = wx.BitmapButton(self, id=-1, bitmap=image1, size=(image1.GetWidth() +40, image1.GetHeight() + 40)) self.button1.SetLabel("Copy") hbox.Add(self.button1, 0, wx.ALIGN_CENTER) self.button1.Bind(wx.EVT_BUTTON, self.OnClicked) imageFile2 = "folder.png" image2 = wx.Image(imageFile2, wx.BITMAP_TYPE_ANY).ConvertToBitmap() self.button2 = wx.BitmapButton(self, id=-1, bitmap=image2, size=(image2.GetWidth() + 40, image1.GetHeight() + 40)) self.button2.SetLabel("Folder") hbox.Add(self.button2, 0, wx.ALIGN_CENTER) self.button2.Bind(wx.EVT_BUTTON, self.OnClicked) imageFile3 = "save.png" image3 = wx.Image(imageFile3, wx.BITMAP_TYPE_ANY).ConvertToBitmap() self.button3 = wx.BitmapButton(self, id=-1, bitmap=image3, size=(image3.GetWidth() + 40, image3.GetHeight() + 40)) self.button1.SetLabel("Save") hbox.Add(self.button3, 0, wx.ALIGN_CENTER) self.button3.Bind(wx.EVT_BUTTON, self.OnClicked) vbox.Add(hbox, 1, wx.ALIGN_CENTER) self.SetSizer(vbox) def OnClicked(self,event): btn = event.GetEventObject().GetLabel() print("Label Of Pressed Button Is", btn) class MyApp(wx.App): def OnInit(self): self.frame = MyFrame(parent=None, title="Bitmap Button") self.frame.Show() return True app = MyApp() app.MainLoop() |
This is our MyFrame class that inherits from wx.Frame. and it is our top level window. and also we create the object of MyPanel in here
1 2 3 |
class MyFrame(wx.Frame): def __init__(self, parent, title): super(MyFrame, self).__init__(parent, title =title, size = (600,400)) |
So this is MyPanel class that inherits from wx.Panel. this is the container class for our widgets also we can create layouts in this MyPanel class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
class MyPanel(wx.Panel): def __init__(self, parent): super(MyPanel, self).__init__(parent) vbox = wx.BoxSizer(wx.VERTICAL) hbox = wx.BoxSizer(wx.HORIZONTAL) imageFile = "copy.png" image1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap() self.button1 = wx.BitmapButton(self, id=-1, bitmap=image1, size=(image1.GetWidth() +40, image1.GetHeight() + 40)) self.button1.SetLabel("Copy") hbox.Add(self.button1, 0, wx.ALIGN_CENTER) self.button1.Bind(wx.EVT_BUTTON, self.OnClicked) imageFile2 = "folder.png" image2 = wx.Image(imageFile2, wx.BITMAP_TYPE_ANY).ConvertToBitmap() self.button2 = wx.BitmapButton(self, id=-1, bitmap=image2, size=(image2.GetWidth() + 40, image1.GetHeight() + 40)) self.button2.SetLabel("Folder") hbox.Add(self.button2, 0, wx.ALIGN_CENTER) self.button2.Bind(wx.EVT_BUTTON, self.OnClicked) imageFile3 = "save.png" image3 = wx.Image(imageFile3, wx.BITMAP_TYPE_ANY).ConvertToBitmap() self.button3 = wx.BitmapButton(self, id=-1, bitmap=image3, size=(image3.GetWidth() + 40, image3.GetHeight() + 40)) self.button1.SetLabel("Save") hbox.Add(self.button3, 0, wx.ALIGN_CENTER) self.button3.Bind(wx.EVT_BUTTON, self.OnClicked) vbox.Add(hbox, 1, wx.ALIGN_CENTER) self.SetSizer(vbox) |
Now let me describe some codes in this MyPanel class. these codes are related to our layouts
1 2 |
vbox = wx.BoxSizer(wx.VERTICAL) hbox = wx.BoxSizer(wx.HORIZONTAL) |
By using this class you can create Bitmap Button
1 2 |
self.button2 = wx.BitmapButton(self, id=-1, bitmap=image2, size=(image2.GetWidth() + 40, image1.GetHeight() + 40)) |
Because in this article we are going to create three Bitmap Buttons, so this is the complete section for three Bitmap Buttons. and also make sure that you have three icon images in your working directory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
imageFile = "copy.png" image1 = wx.Image(imageFile, wx.BITMAP_TYPE_ANY).ConvertToBitmap() self.button1 = wx.BitmapButton(self, id=-1, bitmap=image1, size=(image1.GetWidth() +40, image1.GetHeight() + 40)) self.button1.SetLabel("Copy") hbox.Add(self.button1, 0, wx.ALIGN_CENTER) self.button1.Bind(wx.EVT_BUTTON, self.OnClicked) imageFile2 = "folder.png" image2 = wx.Image(imageFile2, wx.BITMAP_TYPE_ANY).ConvertToBitmap() self.button2 = wx.BitmapButton(self, id=-1, bitmap=image2, size=(image2.GetWidth() + 40, image1.GetHeight() + 40)) self.button2.SetLabel("Folder") hbox.Add(self.button2, 0, wx.ALIGN_CENTER) self.button2.Bind(wx.EVT_BUTTON, self.OnClicked) imageFile3 = "save.png" image3 = wx.Image(imageFile3, wx.BITMAP_TYPE_ANY).ConvertToBitmap() self.button3 = wx.BitmapButton(self, id=-1, bitmap=image3, size=(image3.GetWidth() + 40, image3.GetHeight() + 40)) self.button1.SetLabel("Save") hbox.Add(self.button3, 0, wx.ALIGN_CENTER) self.button3.Bind(wx.EVT_BUTTON, self.OnClicked) vbox.Add(hbox, 1, wx.ALIGN_CENTER) self.SetSizer(vbox) |
This is simple event handling method that we are going to bind that with one of our Bitmap Button
1 2 3 |
def OnClicked(self,event): btn = event.GetEventObject().GetLabel() print("Label Of Pressed Button Is", btn) |
Run the code this is the result for our Bitmap Button
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email