Images in a wx.ScrolledWindow

Hello,

I’ve been struggling with using an image displayed in a wx.ScrolledWindow. The problems I’m having are all refresh(repaint) problems related to scrollbars being moved, window resizing, and incorrect image positioning when a repaint occurs and the sliders are not at 0,0… I’ve looked at some past posts related to this, as well as wxWidgets and wxPython documentation, and the book, but it’s sadly not sinking in. I would most appreciate any suggestions!

I’ve listed my test code below. My platform is Solaris 10, Python 2.4.3, wxPython 2.6.3.3 (a little behind in upgrading), though it is no better on windows.

Thanks in advance.

-Chris Botos

import wx

class ImageWindow(wx.ScrolledWindow):
“”“Window for displaying a bitmapped Image”""

def __init__(self, parent):
    wx.ScrolledWindow.__init_

_(self, parent)
self.SetScrollRate(5,5)
self.Bind(wx.EVT_PAINT, self.OnPaint)

def SetBitmap(self, bitmap):
    self.bitmap = bitmap
    self.SetVirtualSize(bitmap.GetSize())

    # The following does nothing when called before
    # the app starts.
    cdc = wx.ClientDC(self)
    cdc.DrawBitmap(self.bitmap, 0, 0)
   
def OnPaint(self, event):
    dc = wx.BufferedPaintDC(self, self.bitmap)

    ## I have tried it with and without the following two lines
    #x,y = self.GetViewStart()
    #dc.DrawBitmap(self.bitmap, x, y)

class TestFrame(
wx.Frame):
def init(self):
wx.Frame.init(self, None, -1)

    # image is 3072 x 2700
    self.im1 = wx.Image("image.bmp", wx.BITMAP_TYPE_ANY)
    [

self.bm](http://self.bm) = self.im1.ConvertToBitmap()
self.wind = ImageWindow(self)
self.wind.SetSize((600,400))
self.wind.SetBitmap(self.bm)
sizer = wx.BoxSizer
()
sizer.Add(self.wind)
self.SetSizer(sizer)
self.Fit()
self.Bind(wx.EVT_SIZE, self.OnSize)

def OnSize(self, event):
    self.wind.SetSize(self.GetSize

())

if name==‘main’:
app = wx.PySimpleApp()
f = TestFrame()
f.Show()
app.MainLoop()

chris botos wrote:

Hello,

I've been struggling with using an image displayed in a wx.ScrolledWindow. The problems I'm having are all refresh(repaint) problems related to scrollbars being moved, window resizing, and incorrect image positioning when a repaint occurs and the sliders are not at 0,0.. I've looked at some past posts related to this, as well as wxWidgets and wxPython documentation, and the book, but it's sadly not sinking in. I would most appreciate any suggestions!

           def OnPaint(self, event):
        dc = wx.BufferedPaintDC(self, self.bitmap)

        ## I have tried it with and without the following two lines
        #x,y = self.GetViewStart()
        #dc.DrawBitmap(self.bitmap, x, y)

Try it like this, so the buffered DC will call PrepareDC on the paint dc it creates internally:

  dc = wx.BufferedPaintDC(self, self.bitmap, wx.BUFFER_VIRTUAL_AREA)

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Thanks so much Robin. I tested this both on windows and Solaris. On Solaris it works great.

On Windows XP the image is now updated correctly, … but the scroll bar(s) are strange (regardless of the value of the dc style). The horizontal bar is non-existent. The vertical bar works, but it is truncated in the horizontal direction, as if it were slit vertically right down the middle, and only the left half of it put into the frame. Resizing the window makes no difference. Fortunately I don’t currently need this to work in windows, so in a sense I’m all set. I’ll work on it some more as I have a chance.

-Chris Botos

···

On 1/10/07, Robin Dunn robin@alldunn.com wrote:

chris botos wrote:

Hello,

I’ve been struggling with using an image displayed in a
wx.ScrolledWindow. The problems I’m having are all refresh(repaint)
problems related to scrollbars being moved, window resizing, and

incorrect image positioning when a repaint occurs and the sliders are
not at 0,0… I’ve looked at some past posts related to this, as well as
wxWidgets and wxPython documentation, and the book, but it’s sadly not

sinking in. I would most appreciate any suggestions!

def OnPaint(self, event):
    dc = wx.BufferedPaintDC(self, self.bitmap)

    ## I have tried it with and without the following two lines
    #x,y = self.GetViewStart()
    #dc.DrawBitmap(self.bitmap, x, y)

Try it like this, so the buffered DC will call PrepareDC on the paint dc
it creates internally:

    dc = wx.BufferedPaintDC

(self, self.bitmap, wx.BUFFER_VIRTUAL_AREA)