我一般是新來的wxPython和GUI。現在應用程序只顯示一個工具欄,狀態欄和下面的面板。該面板包含一個帶有staticBitmap的boxSizer。我想有一個圖像調整自身每當窗口大小以適應其容器,但我遇到了很多閃爍。當窗口被調整大小(EVT_SIZE火災)
resizeImage()調整大小的面板,以適應新的尺寸,然後秤與scaleImage(圖像wxPython的 - 調整圖像無閃爍?
摘要
resizeImage()被調用),並將其放置到所述staticBitmap
resizeImage()基本上抓住所述圖像對象,調整其大小,它設置爲位圖,然後將其設置爲將被顯示的staticbitmap。
代碼
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])
這工作。謝謝。 – ecb 2011-05-06 01:27:51