【发布时间】:2014-01-26 22:04:01
【问题描述】:
当父 wx.Frame 调整大小时(例如 wxPython for image and buttons (resizable)),我需要能够在 wx.Panel 中重新调整图像(实时)。
此代码现在可以使用,其行为就像在标准照片查看器中一样:图像完全适合父窗口,并且重新缩放尊重纵横比。
import wx
class MainPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1, style=wx.FULL_REPAINT_ON_RESIZE)
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.img = wx.Image('background.png', wx.BITMAP_TYPE_PNG)
self.imgx, self.imgy = self.img.GetSize()
def OnPaint(self, event):
dc = wx.PaintDC(self)
dc.Clear()
x,y = self.GetSize()
posx,posy = 0, 0
newy = int(float(x)/self.imgx*self.imgy)
if newy < y:
posy = int((y - newy) / 2)
y = newy
else:
newx = int(float(y)/self.imgy*self.imgx)
posx = int((x - newx) / 2)
x = newx
img = self.img.Scale(x,y, wx.IMAGE_QUALITY_HIGH)
self.bmp = wx.BitmapFromImage(img)
dc.DrawBitmap(self.bmp,posx,posy)
class MainFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, -1, title='Test', size=(600,400))
self.panel = MainPanel(self)
self.Show()
app = wx.App(0)
frame = MainFrame(None)
app.MainLoop()
在继续实施一些事情之前,我想知道:
- 这是“好方法”吗?
-
FULL_REPAINT_ON_RESIZE可能有点太多(效率低下),但如果没有这个我就做不到,你有改进的想法吗? - 如何跟踪鼠标点击? 示例:我想跟踪一个矩形 (10,20) 到 (50,50) 在原始图像的原始坐标中的点击。我应该将其转换为新坐标(因为图像已重新缩放!)?这意味着我现在应该在非常低的级别上做所有事情......
【问题讨论】: