Search code examples
pythonwxpython

wxpython : Add Background picture and buttons


I am fairly new to wxPython and is trying to place some button over an background image. I was unable to find a container in wxPython that can contain an image and, on top of it some buttons. I am using the below code hence:

import wx

########################################################################
class MainPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)
        self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
        self.frame = parent

        sizer = wx.BoxSizer(wx.VERTICAL)
        hSizer = wx.BoxSizer(wx.HORIZONTAL)

        button1 = wx.Button(self, label='Button1', pos=(100, 100), size=(175, 28))
        sizer.Add(button1, 0, wx.ALL, 5)
        hSizer.Add((1,1), 1, wx.EXPAND)
        hSizer.Add(sizer, 0, wx.TOP, 100)
        hSizer.Add((1,1), 0, wx.ALL, 75)
        self.SetSizer(hSizer)
        self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)

    #----------------------------------------------------------------------
    def OnEraseBackground(self, evt):
        """
        Add a picture to the background
        """
        # yanked from ColourDB.py
        dc = evt.GetDC()

        if not dc:
            dc = wx.ClientDC(self)
            rect = self.GetUpdateRegion().GetBox()
            dc.SetClippingRect(rect)
        dc.Clear()
        bmp = wx.Bitmap("admin.png")
        dc.DrawBitmap(bmp, 0, 0)


########################################################################
class MainFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, size=(320, 243))
        panel = MainPanel(self)        
        self.Center()

########################################################################
class Main(wx.App):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, redirect=False, filename=None):
        """Constructor"""
        wx.App.__init__(self, redirect, filename)
        dlg = MainFrame()
        dlg.Show()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = Main()
    app.MainLoop()

But the issue that button1 is not getting placed properly - no matter whatever I am setting the pos=(100, 100), size=(175, 28) values- it gets placed in same location (?)

Also I do no want the maximize, minimize and restore button options - probably only a button to close the window - how can I remove the default maximize, minimize and restore button from the frame / application window?

=======UPDATE=======

Below is the exact image with text and buttons I need to create the GUI interface - I tried a lot to create buttons same like that or graphics like this but I am unable - I have already posted the code - please let me know how I can achieve this?


Solution

  • I wrote an article on putting a background image on a panel, which should work for your use case as well. You can read it here:

    For removing the various buttons in the title bar, you'll need to mess with the frame's style flags. This other thread talks a bit about that:

    The example there shows the following code:

    frame = wx.Frame( None, title="Hello wxPython", style = wx.CLOSE_BOX | wx.CAPTION | wx.RESIZE_BORDER )
    

    That should get you pretty close to what you want.