fit image on wxpanel

Hi,

I want to display an image such that it fits exactly on a wxpanel. For this I set the size of the wxpanel to the size of the image I want to display. However I see that the panel is slightly smaller than my image. Anyone knows how to solve this ?

regards,

Pluto

import wx

import os

class Panel(wx.Panel):

def __init__(self, parent, path):


    bitmap = wx.Bitmap(path)

    orgWidth = bitmap.GetWidth()

    orgHeight = bitmap.GetHeight()


    panelWidth = orgWidth

    panelHeight = orgHeight


    wx.Panel.__init__(self, parent,-1, size=(panelWidth, panelHeight))


    self.ImgControl = wx.StaticBitmap(self, -1, bitmap)

    self.ImgControl.Center( )

if name == ‘main’:

app = wx.PySimpleApp()

frame = wx.Frame(None, -1, 'Scaled Image', size=(1392, 1040))

panel = Panel(frame, os.path.join( os.getcwd(), "middleWhite.tif") )

frame.Show()

app.MainLoop()

Test.py (656 Bytes)

You should use StaticBitmap for icons, while GenStaticBitmap should be used for images:
from wx.lib import statbmp

self.ImgControl = statbmp.GenStaticBitmap(self.panel, wx.ID_ANY, bitmap)

Not sure why the Panel is smaller, but it might have something to do with the ClientSize vs Size… not sure. It might help if you use sizers, so there you give the ‘automagic’ resizing info to work on. Resizing the frame won’t resize the image though, if that’s what you want… you will need to catch the Frame resize event then manually scale the image based on the new size, then get a new scaled bitmap, then update/set the bitmap to the image control. You probably also don’t want to any of what I just said unless the left mouse button is up (so as you drag the Frame to resize, it won’t generate a new scaled image for each pixel of Frame size that changes… rather, for each resize event that fires, only the last one).

···

On Thursday, October 16, 2014 11:25:40 AM UTC-7, pluto mars wrote:

Hi,

I want to display an image such that it fits exactly on a wxpanel. For this I set the size of the wxpanel to the size of the image I want to display. However I see that the panel is slightly smaller than my image. Anyone knows how to solve this ?

regards,

Pluto

import wx

import os

class Panel(wx.Panel):

def __init__(self, parent, path):


    bitmap = wx.Bitmap(path)

    orgWidth = bitmap.GetWidth()

    orgHeight = bitmap.GetHeight()


    panelWidth = orgWidth

    panelHeight = orgHeight


    wx.Panel.__init__(self, parent,-1, size=(panelWidth, panelHeight))


    self.ImgControl = wx.StaticBitmap(self, -1, bitmap)

    self.ImgControl.Center( )

if name == ‘main’:

app = wx.PySimpleApp()

frame = wx.Frame(None, -1, 'Scaled Image', size=(1392, 1040))

panel = Panel(frame, os.path.join( os.getcwd(), "middleWhite.tif") )

frame.Show()

app.MainLoop()

pluto mars wrote:

Hi,

I want to display an image such that it fits exactly on a wxpanel. For
this I set the size of the wxpanel to the size of the image I want to
display. However I see that the panel is slightly smaller than my image.
Anyone knows how to solve this ?

frame = wx.Frame(None, -1, 'Scaled Image', size=(1392, 1040))
panel = Panel(frame, os.path.join( os.getcwd(), "middleWhite.tif") )

Does the size of the image happen to be 1392x1040? If so then you are setting the total size of the frame to that size, including the borders and caption bar, so it is going to clip the panel. As Nathan mentioned, the best way to do it is to use sizers to manage the size of windows based on the needs of their children, although in this case you could just do something like this before the Show:

  frame.SetClientSize(panel.GetSize())

The client size for the frame is the area inside the borders.

···

--
Robin Dunn
Software Craftsman

Hi pluto,

You force a size of your frame “size=(1392, 1040)”, may be your image is bigger than that.
I alway use a sizer with a panel even there is only one child and then add the child to take full size.

I hope it could help.

Regards

Stephan

···

Le vendredi 17 octobre 2014 04:25:40 UTC+10, pluto mars a écrit :

Hi,

I want to display an image such that it fits exactly on a wxpanel. For this I set the size of the wxpanel to the size of the image I want to display. However I see that the panel is slightly smaller than my image. Anyone knows how to solve this ?

regards,

Pluto

import wx

import os

class Panel(wx.Panel):

def __init__(self, parent, path):


    bitmap = wx.Bitmap(path)

    orgWidth = bitmap.GetWidth()

    orgHeight = bitmap.GetHeight()


    panelWidth = orgWidth

    panelHeight = orgHeight


    wx.Panel.__init__(self, parent,-1, size=(panelWidth, panelHeight))


    self.ImgControl = wx.StaticBitmap(self, -1, bitmap)

    self.ImgControl.Center( )

if name == ‘main’:

app = wx.PySimpleApp()

frame = wx.Frame(None, -1, 'Scaled Image', size=(1392, 1040))

panel = Panel(frame, os.path.join( os.getcwd(), "middleWhite.tif") )

frame.Show()

app.MainLoop()