2

How to add background image to panel1? Which command is there?

Code:

import wx
import wx
appy=wx.App()

class cg(wx.Frame) :
     def __init__(self,parent,id) :
         wx.Frame.__init__(self,parent,id,'GPA',pos=(1000,600),size=(600,400))
         #splitter = wx.SplitterWindow(self, -1)
         panel1 = wx.Panel(self)
         panel2 = wx.Panel(panel1, -1,size=(600,200),style=wx.BORDER_SUNKEN)
         panel3 = wx.Panel(panel1, -1,pos=(0,200),size=(600,200),style=wx.BORDER_SUNKEN)
         #panel3=wx.panel(panel1,-1,pos=(300,200),size=(600,200),style=wx.BORDER_SUNKEN)
         #panel13 = wx.Panel(panel1, -1, style=wx.BORDER_SUNKEN)
         #panel13 = wx.Panel(panel1, -1, style=wx.BORDER_SUNKEN)
         #panel13 = wx.Panel(panel1, -1, style=wx.BORDER_SUNKEN)

         #button1=wx.Button(panel1,label='exit',pos=(10,10),size=(10,10))
         #self.cnt1=wx.TextCtrl(panel1,pos=(40,60),size=(120,30))

if __name__=='__main__' :

     app=wx.PySimpleApp()
     frame=cg(parent=None,id=-1)
     frame.Show()
     app.MainLoop()

2 Answers 2

5

The corrected example from The Mouse vs. The Python.

import wx
 
########################################################################
class MainPanel(wx.Panel):
    """"""
 
    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)
        self.SetBackgroundStyle(wx.BG_STYLE_PAINT) # Was wx.BG_STYLE_CUSTOM)
        self.frame = parent
 
        sizer = wx.BoxSizer(wx.VERTICAL)
        hSizer = wx.BoxSizer(wx.HORIZONTAL)
 
        for num in range(4):
            label = "Button %s" % num
            btn = wx.Button(self, label=label)
            sizer.Add(btn, 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("butterfly.jpg")
        dc.DrawBitmap(bmp, 0, 0)
 
 
########################################################################
class MainFrame(wx.Frame):
    """"""
 
    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, size=(600,450))
        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()
Sign up to request clarification or add additional context in comments.

4 Comments

Setting self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM) no longer works. That line should be removed or use self.SetBackgroundStyle(wx.BG_STYLE_ERASE) but given that that is the default, best just leave it out.
@RolfofSaxony: Thanks for bringing this up. Actually, since we are defining an EVT_ERASE_BACKGROUND handler this is now recommended to be BG_STYLE_PAINT, see docs.wxpython.org/…
@SteveBarnes then why does BG_STYLE_PAINT not work and BG_STYLE_ERASE does?
@VaidøtasI. I don't know I am just going on the documented behaviour.
2

A simple search would have brought you to this answer from The Mouse vs. The Python.

# create a background image on a wxPython panel
# and show a button on top of the image

import wx

class Panel1(wx.Panel):
    """class Panel1 creates a panel with an image on it, inherits wx.Panel"""
    def __init__(self, parent, id):
        # create the panel
        wx.Panel.__init__(self, parent, id)
        try:
            # pick an image file you have in the working folder
            # you can load .jpg  .png  .bmp  or .gif files
            image_file = 'roses.jpg'
            bmp1 = wx.Image(image_file, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
            # image's upper left corner anchors at panel coordinates (0, 0)
            self.bitmap1 = wx.StaticBitmap(self, -1, bmp1, (0, 0))
            # show some image details
            str1 = "%s  %dx%d" % (image_file, bmp1.GetWidth(), bmp1.GetHeight()) 
            parent.SetTitle(str1)
        except IOError:
            print "Image file %s not found" % imageFile
            raise SystemExit

        # button goes on the image --> self.bitmap1 is the parent
        self.button1 = wx.Button(self.bitmap1, id=-1, label='Button1', pos=(8, 8))

app = wx.PySimpleApp()
# create a window/frame, no parent, -1 is default ID
# change the size of the frame to fit the backgound images
frame1 = wx.Frame(None, -1, "An image on a panel", size=(350, 400))
# create the class instance
panel1 = Panel1(frame1, -1)
frame1.Show(True)
app.MainLoop()

2 Comments

can u please tell how to load the jpg image?
Have you read the code or visited the site? It is explained very clearly: image_file = 'roses.jpg'. It looks in the same file as the script is run. If you want to provide a full path, look into os.path.join. Would appreciate you selecting this as best-answer too if it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.