wxpython - 没有闪烁的大小调整图像?

时间:2011-05-01 00:09:07

标签: python image user-interface wxpython flicker

我是wxPython和GUI的新手。现在,应用程序只显示工具栏,状态栏和以下面板。该面板包含一个带有staticBitmap的boxSizer。我试图在窗口调整大小时让图像调整大小以适应容器,但是我遇到了很多闪烁。

摘要
调整窗口大小时调用resizeImage()(EVT_SIZE触发)
resizeImage()调整面板的大小以适应新的尺寸,然后使用scaleImage()缩放图像,并将其放入staticBitmap

resizeImage()基本上抓取图像对象,调整大小,将其设置为位图,然后将其设置为要显示的静态位图。

代码

class Canvas(wx.Panel):
"""Panel used to display selected images"""

#---------------------------------------------------------------------------
def __init__(self, parent):
    """Constructor"""
    wx.Panel.__init__(self, parent)

    # Globals
    self.image        = wx.EmptyImage(1,1)
    self.control    = wx.StaticBitmap(self, wx.ID_ANY, 
                                     wx.BitmapFromImage(self.image))    
    self.background    = wx.BLACK
    self.padding    = 5
    self.imageList    = []
    self.current    = 0
    self.total        = 0

    # Register Events
    Publisher().subscribe(self.onLoadDirectory, ("load directory"))
    Publisher().subscribe(self.resizeImage, ("resize window"))

    # Set Layout
    self.mainSizer = wx.BoxSizer(wx.VERTICAL)
    self.mainSizer.Add(self.control, 1, wx.ALL|wx.CENTER|wx.EXPAND,
            self.padding)
    self.SetSizer(self.mainSizer)
    self.SetBackgroundColour(self.background)

#---------------------------------------------------------------------------
def scaleImage(self, image, maxWidth, maxHeight):
    """asd"""
    width    = image.GetWidth()
    height    = image.GetHeight()
    ratio    = min( maxWidth / width, maxHeight/ height );
    image    = image.Scale(ratio*width, ratio*height, wx.IMAGE_QUALITY_HIGH)
    result    = wx.BitmapFromImage(image)

    return result

#---------------------------------------------------------------------------
def loadImage(self, image):
    """Load image"""
    self.image = wx.Image(image, wx.BITMAP_TYPE_ANY)
    bmp = wx.BitmapFromImage(self.image)
    w, h = self.mainSizer.GetSize()
    w = w - self.padding*2
    h = h - self.padding*2
    bmp = self.scaleImage(self.image, w, h)        
    self.control.SetBitmap(bmp)

#---------------------------------------------------------------------------
def getImageIndex(self, path):
    """Retrieve index of image from imagePaths"""
    i = 0
    for image in self.imagePaths:
        if image == path:
            return i
        i += 1
    return -1

#---------------------------------------------------------------------------
def resizeImage(self, event):
    self.SetSize(event.data)
    if self.total:
        w = event.data[0] - self.padding*2
        h = event.data[1] - self.padding*2
        bmp = self.scaleImage(self.image, w, h)
        self.control.SetBitmap(bmp)

#---------------------------------------------------------------------------
def onLoadDirectory(self, event):
    """Load the image and compile a list of image files from directory"""
    self.folderPath        = os.path.dirname(event.data)
    self.imagePaths        = glob.glob(self.folderPath + "\\*.jpg")
    self.total            = len(self.imagePaths)
    self.current        = self.getImageIndex(event.data)
    self.SetSize(self.GetSize())
    self.loadImage(self.imagePaths[self.current])

2 个答案:

答案 0 :(得分:1)

在resizeImage方法中,添加冻结和解冻可能会有所帮助,如下所示:

def resizeImage(self, event):
    self.SetSize(event.data)
    if self.total:
        w = event.data[0] - self.padding*2
        h = event.data[1] - self.padding*2
        self.Freeze()
        bmp = self.scaleImage(self.image, w, h)
        self.control.SetBitmap(bmp)
        self.Thaw()

答案 1 :(得分:1)

drawing DC上尝试double buffered,而不是使用StaticBitmap。