Why isn't my jpg being drawn on the frame

I have included my code below. I am trying to size the image to the client area size of a frame and then.draw the jpg image on the client area of the frame.

The code below only produces a frame with a grey background. Why am I not getting my image?

import wx
import os

class Mywin(wx.Frame):

   def __init__(self, parent, title):
      super(Mywin, self).__init__(parent, title = title,size = (500,300))
      back_Image = os.path.dirname(os.path.abspath(__file__)) + '\\Skins\\' + 'picture.jpg'
      bmp_Img=wx.Image(back_Image, wx.BITMAP_TYPE_ANY)
      frame_size = self.GetSize()
      frame_h = frame_size[0]
      frame_w = frame_size[1]
      final_ImgResized = bmp_Img.Scale(frame_h,frame_w)
      wx.dc = wx.ClientDC(self)
      wx.dc.DrawBitmap(wx.Bitmap(final_ImgResized),0,0,True)
      self.Show(True)

ex = wx.App()
Mywin(None,'Drawing demo')
ex.MainLoop()

wx.ClientDC is meant for transitory/temporary drawing not for things that need to be visible long term. Those things need to be drawn from a wx.EVT_PAINT event. In addition, any drawing done while a window is not visible is basically ignored on most platforms.

So what is happening in your example is that nothing is drawn, then the frame is shown, then there is a EVT_PAINT event, and the default paint handler simply draws the frame’s default background. My suggestion is to continue to load the image and convert to a wx.Bitmap in the frame’s __init__ method, saving it in an attribute of self, like self.bmp. Then you can bind an event handler for wx.EVT_PAINT and in that handler create a wx.PaintDC and call its DrawBitmap method. You may also want to add a wx.EVT_SIZE event handler and rescale your image to the new frame size.

Oh, one more thing:

    frame_size = self.GetSize()

A frame’s size is not necessarily the size available for drawing in (think about the frame’s possible borders and caption bar). You should probably use GetClientSize here.

1 Like

Thanks, I will try this approach.

Thank you! All is working except when resizing I see the original picture and artifacts for each redwaw. Am I missing something to perhaps clear the area before a redraw? I thought it would just overwrite.

Edit: Nevermind. The redraw needed ClientDC instead of PaintDC. All is well now. Thank you so much for your help.