Image not Showing

So, I have finally gotten my GUI code to work, but now the image that is supposed to be in the scrolled panel never shows up. This app has the same class structure as my full code and loads the image in the same way. I’m sure I’m just making a simple mistake but I could use some help.

GuidoInTheComfyChair.PNG

guido-van-rossum.jpg

SampleImageCode.txt (1.72 KB)

Hi,

So, I have finally gotten my GUI code to work, but now the image that is
supposed to be in the scrolled panel never shows up. This app has the same
class structure as my full code and loads the image in the same way. I'm
sure I'm just making a simple mistake but I could use some help.

An Image/Bitmap is a data class, it is not a Window object

"""
img = wx.Image(imageFile)
self.SetVirtualSize(img.GetSize())
self.SetScrollRate(20,20)
self.sizer.Add(img.ConvertToBitmap().GetSize())
"""

You cant add a wx.Image to a sizer would think that you should have
had a crash at the sizer.Add line here.

If you want to display the image in the panel you can just create a
StaticBitmap with it and then add the StaticBitmap control to the
sizer.

Cody

···

On Fri, May 6, 2011 at 2:10 PM, PRobbins <pjrobbins89@gmail.com> wrote:

PRobbins wrote:

So, I have finally gotten my GUI code to work, but now the image that
is supposed to be in the scrolled panel never shows up. This app has
the same class structure as my full code and loads the image in the
same way. I'm sure I'm just making a simple mistake but I could use
some help.

You are creating an image, but you never put that image anywhere it can
be displayed. What you're adding to the sizer is the SIZE of the
image. Did you mean to add a StaticBitmap to the panel to hold the
actual image?

If I add this to the scrollPanel constructor:
        self.img = wx.StaticBitmap( self, -1 )
        self.img.SetBitmap( img.ConvertToBitmap() )
and change the next to the last line to:
        self.sizer.Add(self.img)

and then change changeImage to:
    def changeImage(self):
        newImage = r'xxxxx.jpg'
        img = wx.Image(newImage)
        bmp = img.ConvertToBitmap()
        self.img.SetBitmap( bmp )

Then this works mostly as expected.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

He's adding the image's size there, so it ends up being a spacer, which is why there is no crash.

···

On 5/6/11 12:16 PM, Cody Precord wrote:

Hi,

On Fri, May 6, 2011 at 2:10 PM, PRobbins<pjrobbins89@gmail.com> wrote:

So, I have finally gotten my GUI code to work, but now the image that is
supposed to be in the scrolled panel never shows up. This app has the same
class structure as my full code and loads the image in the same way. I'm
sure I'm just making a simple mistake but I could use some help.

An Image/Bitmap is a data class, it is not a Window object

"""
img = wx.Image(imageFile)
self.SetVirtualSize(img.GetSize())
self.SetScrollRate(20,20)
self.sizer.Add(img.ConvertToBitmap().GetSize())
"""

You cant add a wx.Image to a sizer would think that you should have
had a crash at the sizer.Add line here.

--
Robin Dunn
Software Craftsman

Ok, changing it to a static bitmap seemed to work partially. Now I’m running into a problem where only a small sliver of the image is displayed and there is no vertical scrollbar(see attached image). Here is my scrollPanel class code and the Main Frame’s sizer code. I’ve tried changing the SetSizerAndFit to SetSizer so that doesn’t seem to be the problem.

class iBrowserImagePanel(wx.ScrolledWindow):
def init(self, parent):
wx.ScrolledWindow.init(self, parent)
self.parent = parent
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.bmp = wx.StaticBitmap(self, -1, wx.EmptyBitmap(500,500))
self.sizer.Add(self.bmp)
self.SetSizer(self.sizer)

def loadCurrentImage(self):
    '''
    Loads the current image based off Workspace.currentImageIndex.
    This function is called after every image change.
    '''
    try:
        self.currentImage = Workspace.data.getImageDirectory() + os.sep + Workspace.data.getImageName(Workspace.currentImageIndex)
    except:
        Workspace.currentImageIndex = 1
    self.img = simple_image.Image()
    if enhanceTool.norm.GetValue()==True:
        self.img.normalize('RGB')
    if enhanceTool.norm.GetValue()==False:
        self.img.normalize(None)
    if enhanceTool.eq.GetValue()==True:
        self.img.equalize('RGB')
    if enhanceTool.eq.GetValue()==False:
        self.img.equalize(None)
    if enhanceTool.sharp.GetValue()==True:
        self.img.enhanceSharpness(2.0)
    if enhanceTool.sharp.GetValue()==False:
        self.img.enhanceSharpness(1.0)
    if enhanceTool.invert.GetValue()==True:
        self.img.invert(True)
    if enhanceTool.invert.GetValue()==False:
        self.img.invert(False)
    self.img.load(self.currentImage)
    imageBrowser.iWidth = self.img.wx().GetWidth()
    imageBrowser.iHeight = self.img.wx().GetHeight()
    self.img = self.img.wx().Scale(imageBrowser.iWidth*Workspace.zoomScale, imageBrowser.iHeight*Workspace.zoomScale).ConvertToBitmap()
    self.bmp.SetBitmap(self.img)
    self.SetVirtualSize(self.img.GetSize())
    self.SetScrollRate(20,20)
    self.parent.Refresh()
    self.parent.Update()

Main Frame’s sizer code:
self.browserPanel = iBrowserWidgetPanel(self, self.WS)
self.imagePanel = iBrowserImagePanel(self)
self.imagePanel.loadCurrentImage()

    self.browserSizer = wx.BoxSizer(wx.VERTICAL)
    self.browserSizer.Add(self.imagePanel, wx.EXPAND)
    self.browserSizer.Add(self.browserPanel, wx.EXPAND)
    self.SetSizerAndFit(self.browserSizer)
         
    self.Update()

Woops forgot to add the screenshot

PictureSliver.JPG

Try using the WIT to help you figure out what is occupying that space and/or why the sizer is not expanding the scrolled window. http://wiki.wxpython.org/Widget_Inspection_Tool

···

On 5/6/11 12:32 PM, PRobbins wrote:

Ok, changing it to a static bitmap seemed to work partially. Now I'm
running into a problem where only a small sliver of the image is
displayed and there is no vertical scrollbar(see attached image). Here
is my scrollPanel class code and the Main Frame's sizer code. I've tried
changing the SetSizerAndFit to SetSizer so that doesn't seem to be the
problem.

--
Robin Dunn
Software Craftsman

Thanks a bunch! I've got it working now.

···

On May 6, 3:50 pm, Robin Dunn <ro...@alldunn.com> wrote:

On 5/6/11 12:32 PM, PRobbins wrote:

> Ok, changing it to a static bitmap seemed to work partially. Now I'm
> running into a problem where only a small sliver of the image is
> displayed and there is no vertical scrollbar(see attached image). Here
> is my scrollPanel class code and the Main Frame's sizer code. I've tried
> changing the SetSizerAndFit to SetSizer so that doesn't seem to be the
> problem.

Try using the WIT to help you figure out what is occupying that space
and/or why the sizer is not expanding the scrolled window.http://wiki.wxpython.org/Widget_Inspection_Tool

--
Robin Dunn
Software Craftsmanhttp://wxPython.org

Ok, changing it to a static bitmap seemed to work partially. Now I'm
running into a problem where only a small sliver of the image is
displayed and there is no vertical scrollbar(see attached image).

This may be your problem:

self.browserSizer.Add(self.imagePanel, wx.EXPAND)
self.browserSizer.Add(self.browserPanel, wx.EXPAND)

the second argument to an Add call is the "option" paramter -- poorly named. it specifies how much weight a SizerItem should get when expanding -- 0 "is don't stretch" anyting above zero is compared to the other items. wx.EXPAND is a flag -- it goes in the third argument.

so you probably want like:

self.browserSizer.Add(self.imagePanel, 1, wx.EXPAND)
self.browserSizer.Add(self.browserPanel, 1, wx.EXPAND)

Note: if you made this a full, self contained app, I'd test it before posting what may not be correct after all...

-Chris

···

On 5/6/11 12:32 PM, PRobbins wrote:

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

PRobbins wrote:

    Ok, changing it to a static bitmap

seemed to work partially. Now I’m running into a problem where
only a small sliver of the image is displayed and there is no
vertical scrollbar(see attached image). Here is my scrollPanel
class code and the Main Frame’s sizer code. I’ve tried changing
the SetSizerAndFit to SetSizer so that doesn’t seem to be the
problem.

I hope you do not mind if I make some stylistic comments.  It's a

bad habit of mine.

class iBrowserImagePanel(wx.ScrolledWindow):

      def __init__(self, parent):

          wx.ScrolledWindow.__init__(self, parent)

          self.parent = parent

          self.sizer = wx.BoxSizer(wx.VERTICAL)

          self.bmp = wx.StaticBitmap(self, -1,

wx.EmptyBitmap(500,500))

          self.sizer.Add(self.bmp)

          self.SetSizer(self.sizer)
If the bitmap is the only thing in the scrolled window, you

shouldn’t need a sizer here at all.

try:

              self.currentImage = Workspace.data.getImageDirectory()
  • os.sep +
    Workspace.data.getImageName(Workspace.currentImageIndex)

            except:
    
                Workspace.currentImageIndex = 1
    
How can that first line fail?  In any case, if it DOES fail, this

will leave self.currentImage unchanged. It won’t set
self.currentImage to match ImageIndex 1.

if enhanceTool.norm.GetValue()==True:

              self.img.normalize('RGB')

          if enhanceTool.norm.GetValue()==False:

              self.img.normalize(None)

          if enhanceTool.eq.GetValue()==True:

              self.img.equalize('RGB')

          if enhanceTool.eq.GetValue()==False:

              self.img.equalize(None)

          if enhanceTool.sharp.GetValue()==True:

              self.img.enhanceSharpness(2.0)

          if enhanceTool.sharp.GetValue()==False:

              self.img.enhanceSharpness(1.0)

          if enhanceTool.invert.GetValue()==True:

              self.img.invert(True)

          if enhanceTool.invert.GetValue()==False:

              self.img.invert(False)
In a gross overgeneralization, any time you compare with ==True and

==False, it’s a problem. It’s better to think in terms of “true
values” and “false” values. Further, it’s more efficient to use
if/else than it is to do “if == True” and “if == False”. For
example:

    if enhanceTool.norm.GetValue():

        self.img.normalize('RGB')

    else:

        self.img.normalize(None)

    if enhanceTool.eq.GetValue():

        self.img.equalize('RGB')

    else:

        self.img.equalize(None)

Or, preferable to some:

    self.img.normalize( 'RGB' if enhanceTool.norm.GetValue() else

None )

    self.img.equalize( 'RGB' if enhanceTool.eq.GetValue() else None

)

and the last one can be a single expression:

    self.img.invert( enhanceTool.invert.GetValue() )
···
-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com