I'm creating a graphic, later to be a photo, and placing it onto a
panel. While this works fine in itself, there are annoying artifacts
during subsequent frame resizings. A wx.StaticBitmap is created to
hold the image.
Would creating a custom bitmapped button or frame eliminate the
"ghosts" of all the old images ? Is there a "prescribed" technique ?
(running 32-bit python/Pil/wxPython on 64-bit Win7)
···
==================================================
import wx
import Image # rectangle image creation
import random # rectangle colors
#--------------------------------------------------------------------
class MainFrame( wx.Frame ):
def __init__( self, parent, id, title ):
wx.Frame.__init__( self, parent,-4, title, size=(400, 400),
pos=(800, 100),
style=wx.DEFAULT_FRAME_STYLE )
self.panel = wx.Panel( self, -1 )
self.backgroundColor = NewRandomColor()
self.panel.SetBackgroundColour( self.backgroundColor )
self.currClientSizePrev = self.GetClientSize() # Init
self.Show( True )
#--------------------
# Overlay a bordered rectangle on the panel
# after a retriggerable one-shot timeout and
# after the last resize event. This avoids unecessary
redraws.
self.hasBeenResized = False
self.Bind( wx.EVT_SIZE, self.OnSize ) # this event only sets
a redraw flag
self.timer = wx.Timer( self ) # redraw only on
timeout and redraw flag is set
self.Bind (wx.EVT_TIMER, self.OnTimer )
self.timerInterval = 200 #
delay value chosen heuristically
self.timer.Start( self.timerInterval, oneShot=True ) #
Enable one-shot timer IRQ's
#end def __init__
#---------------------------------------------
def DisplayBorderedRectangle( self, size, color, pos ):
if ( (size[0] > 0) and (size[1] > 0) ) :
imgblank = Image.new( 'RGB', size )
margin = 2 # Leave a
black border
imgblank.paste( color, (margin, margin,
size[0]-margin, size[1]-margin ) )
imgdata = imgblank.tostring( 'raw', 'RGB' )
emptyimage = wx.EmptyImage( size[0], size[1] ) # An
image display container
emptyimage.SetData( imgdata ) # data
conversion
self.blankbitmap = emptyimage.ConvertToBitmap()
wx.StaticBitmap( self.panel, -1, self.blankbitmap,
size=size, pos=pos )
self.panel.Refresh()
#end if
#end def
#--------------------------------------------
def OnSize( self, event ):
currClientSize = self.GetClientSize()
if ( currClientSize != self.currClientSizePrev ) :
self.hasBeenResized = True
self.currClientSizePrev = currClientSize
self.timer.Start( self.timerInterval,
oneShot=True )
#end if
event.Skip( True )
#end def
#--------------------------------------------
def OnTimer( self, event ):
if self.hasBeenResized :
# Display a new graphic
currClientSize = self.GetClientSize()
currClientSizeX, currClientSizeY = (currClientSize[0],
currClientSize[1])
reductionBorderX = 30
reductionBorderX, reductionBorderY = (25, 25)
newImageSizeX = currClientSizeX - reductionBorderX
newImageSizeY = currClientSizeY - reductionBorderY
newImageSize = ( newImageSizeX, newImageSizeY )
newImagePos = ( reductionBorderX/2, reductionBorderY/2 )
newImageColor = NewRandomColor()
# "Erase" the viewable area
self.DisplayBorderedRectangle( currClientSize,
self.backgroundColor, (0, 0) )
# Paint the new graphic
self.DisplayBorderedRectangle( newImageSize,
newImageColor, newImagePos )
self.hasBeenResized = False # make ready for next
resize/repaint
#end if
self.timer.Start( self.timerInterval, oneShot=True )
event.Skip( True )
#end def
#end class MainFrame
#------------------------------------------------------------------------------
def NewRandomColor():
return (random.randint(0, 255), random.randint(0, 255),
random.randint(0, 255))
#end def
#==============================================================================
MyApp = wx.PySimpleApp( False ) # Send errors to command window, not
a pop-up.
MyFrame = MainFrame( None, -1, 'Image Fit' )
MyApp.MainLoop()
--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en