views:

104

answers:

1

I'm trying to place an image panel on a form such that when a button is clicked the 64x64 image that's put on the panel at program start is replaced with a bigger 320x224 image - the pixel sizes aren't so much important as they are being different sizes. I've ALMOST got it - right now the images both load and it does indeed put the second one up when the button's clicked - unfortunately it's the top left 64x64 of the second image, not the whole thing.

It must be possible to resize the panel so the whole image can be viewed, surely? Here's my code as is:

#First we create our form elements. This app has a label on top of a button, both below a panel with an image on it, so we create a sizer and the elements
self.v_sizer = wx.BoxSizer(wx.VERTICAL)
self.imagePanel = wx.Panel(self, -1)
self.FileDescriptionText = wx.StaticText(self, label="No file loaded")
self.openFileDialog = wx.Button(self, label="Load a file", size=(320,40))
#Bind the button click to our press function
self.openFileDialog.Bind(wx.EVT_BUTTON, self.onOpenFileDialog)

#That done, we need to construct the form. First, put the panel, button and label in the vertical sizer...
self.v_sizer.Add(self.imagePanel, 0, flag=wx.ALIGN_CENTER)
self.v_sizer.Add(self.openFileDialog, 0, flag=wx.ALIGN_CENTER)
self.v_sizer.Add(self.ROMDescriptionText, 0, flag=wx.ALIGN_CENTER)
#then assign an image for the panel to have by default, and apply it
self.imageToLoad = wx.Image("imgs/none_loaded.png", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
self.imageCtrl = wx.StaticBitmap(self.imagePanel, -1, self.imageToLoad, (0, 0), (self.imageToLoad.GetWidth(), self.imageToLoad.GetHeight()))

#Set the sizer to be owned by the window
self.SetSizer(self.v_sizer)
#Set the current window size to the size of the sizer
self.v_sizer.Fit(self)
#Set the Minimum size of the window to the current size of the sizer
self.SetMinSize(self.v_sizer.GetMinSize())


def onOpenFileDialog(self, event):
    img = wx.Image("imgs/title.png", wx.BITMAP_TYPE_ANY)
    self.imageCtrl.SetBitmap(wx.BitmapFromImage(img))
    self.imagePanel.Refresh()

(It's called onOpenFileDialog as eventually it'll be picking the images from a combobox path.)

How can I edit the onOpenFileDialog method such as it finds the image size first, like it does in the self.imageCtrl line in the initial form element creation? I cannot find a way of doing it.

+2  A: 

Try calling self.v_sizer.Fit(self) at the end of your onOpenFileDialog() method

volting
Thank you, that's got it! :)
Glad to hear that :)
volting