Q about images, Refresh() and Layout()

Michael Eager <[email protected]>
Newsgroups gmane.comp.python.wxpython
Message-ID <[email protected]>
I'm pretty confused about the differences between the 
wx.Frame::Refresh() and Layout() functions.  And maybe Update(), as well.

I have an small test application where I create a panel and add an 
wx.StaticBitmap to it, using a BoxSizer to center the image on the 
panel.  Initially, the image is blank.  When I start the app, the blank 
image is displayed centered on the screen as expected.

When I update the image control with a wx.Bitmap, and then call 
Refresh(), the updated image is displayed in the top left corner.  The 
new image fits within the original blank image, although it may not be 
exactly the same size.  If I call Layout(), the updated image is 
displayed correctly.

My expectation was that calling Refresh() would redraw the image (and 
everything else on the panel) without changing where the widget was 
located.  That doesn't appear to be the case.  Calling Layout() should 
recalculate the position of widgets, without redrawing the panel.  That 
also isn't happening in the way I expected.

Can someone give me a clue what is really happening?

I've attached my test program.

Is there a way to update the image in a StaticBitmap without redrawing 
everything in the panel or recomputing the layout?  For example, if I 
have a panel with a number of thumbnail images, it seems that I would 
want to update each one without having to redo the layout.

-- 
Michael Eager

-- 
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/d133eb5d-fd87-3f9d-5166-6150ac295245%40eagercon.com.
try.py (text/x-python, 2.1 KB)
import wx

class MainFrame(wx.Frame):
    def __init__(self):
        super().__init__(None, -1, title="File Browser", size=(1000,1000))

        #self.image_panel = ImagePanel(self, size=(200, 200))
        self.image_panel = ImagePanel(self)

        browseBtn = wx.Button(self, label='Browse')
        browseBtn.Bind(wx.EVT_BUTTON, self.onBrowse)

        vbox = wx.BoxSizer(wx.VERTICAL, )
        vbox.Add(self.image_panel, 1, wx.ALL|wx.EXPAND, 5)
        vbox.Add(browseBtn, 0, wx.ALL, 5)

        self.SetSizer(vbox)
        self.Layout()
        self.Show()

    def onBrowse(self, event):
        """
        Browse for file
        """
        wildcard = "JPEG files (*.jpg)|*.jpg"
        dialog = wx.FileDialog(None, "Choose a file",
                               wildcard=wildcard,
                               style=wx.FD_OPEN)
        if dialog.ShowModal() == wx.ID_OK:
            self.image_panel.loadImage(dialog.GetPath())
        dialog.Destroy()

class ImagePanel(wx.Panel):
    def __init__(self, parent):
        super().__init__(parent, style=wx.BORDER_DOUBLE)

        self.maxsize = 200

        img = wx.Image((400,400))
        empty = wx.EmptyBitmap(200,200)
        dc = wx.MemoryDC(empty)
        dc.SetBackground(wx.Brush("white"))
        dc.Clear()
        self.image_ctrl = wx.StaticBitmap(self, bitmap=empty)

        main_sizer = wx.BoxSizer(wx.VERTICAL)
        main_sizer.Add(self.image_ctrl, 1, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.SHAPED, 5)

        self.SetSizerAndFit(main_sizer)

    def loadImage(self, filepath):
        img = wx.Image(filepath, wx.BITMAP_TYPE_ANY)

        w = img.GetWidth()
        h = img.GetHeight()
        print ("w = %d, h = %d" % (w, h))

        if w > h:
            W = self.maxsize
            H = self.maxsize * h / w
        else:
            H = self.maxsize
            W = self.maxsize * w / h
        img = img.Scale(int(W), int(H))

        self.image_ctrl.SetBitmap(wx.Bitmap(img))
        #self.Refresh()
        self.Layout()

def main():
    app = wx.App()
    fr = MainFrame()
    fr.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.