2016-01-22 41 views
1

我想將圖像放入wxPanel的中心,我在考慮調整圖像的大小以適合wxPanel但我無法動態獲取wxPanel的大小。這裏是一個測試代碼。wxpython:我如何獲得面板的大小或讓圖像適合面板的大小

#!/usr/bin/python 

# wxboxsizer.py 

import wx 

class MyFrame(wx.Frame): 
    def __init__(self, parent, id, title): 
     wx.Frame.__init__(self, parent, id, title, (-1, -1), wx.Size(500, 500)) 
     panel1 = wx.Panel(self, -1,style= wx.SIMPLE_BORDER) 
     panel2 = wx.Panel(self, -1,style= wx.SIMPLE_BORDER) 
     panel3 = wx.Panel(self, -1,style= wx.SIMPLE_BORDER) 
     box = wx.BoxSizer(wx.VERTICAL) 
     box.Add(panel1, 1,flag = wx.EXPAND|wx.ALL,border = 3) 
     box.Add(panel2, 1,flag = wx.EXPAND|wx.ALL,border = 3) 
     box.Add(panel3, 1,flag = wx.EXPAND |wx.ALL,border = 3) 
     self.SetSizer(box) 


class MyApp(wx.App): 
    def OnInit(self): 
     frame = MyFrame(None, -1, 'wxboxsizer.py') 
     frame.Show(True) 
     return True 

app = MyApp(0) 
app.MainLoop() 

這個代碼可以讓3個板完美地契合了框架,但我怎麼能得到面板的大小動態地。 此外,如何將圖像放入面板中,讓圖像動態地貼合面板。

我的解決方案:我剛剛得到一個方法來獲取面板的大小,當框架調整大小。

#!/usr/bin/python 

# wxboxsizer.py 

import wx,os 

class MyFrame(wx.Frame): 
    def __init__(self, parent, id, title): 
     wx.Frame.__init__(self, parent, id, title, (-1, -1), wx.Size(500, 500)) 
     self.panel1 = wx.Panel(self, -1,style= wx.SIMPLE_BORDER) 
     panel2 = wx.Panel(self, -1,style= wx.SIMPLE_BORDER) 
     panel3 = wx.Panel(self, -1,style= wx.SIMPLE_BORDER) 
     box = wx.BoxSizer(wx.VERTICAL) 
     box.Add(self.panel1, 1,flag = wx.EXPAND|wx.ALL,border = 3) 
     box.Add(panel2, 1,flag = wx.EXPAND|wx.ALL,border = 3) 
     box.Add(panel3, 1,flag = wx.EXPAND |wx.ALL,border = 3) 

     self.Bind(wx.EVT_SIZE, self.OnSize) 
     self.SetSizer(box) 

    def OnSize(self, event): 
     size = self.GetSize() 
     size2 = self.panel1.GetSize() 
     print size,size2 
     event.Skip() 


class MyApp(wx.App): 
    def OnInit(self): 
     frame = MyFrame(None, -1, 'wxboxsizer.py') 
     frame.Show(True) 
     return True 

app = MyApp(0) 
app.MainLoop() 

此代碼可以顯示面板的尺寸,當該幀被重新調整大小。

+0

如果面板沒有調整大小,可以在OnSize方法中調用self.Layout()和/或self.Fit()。對於圖像,您可以使用wx.Image來保存圖像對象,並將其放入wx.StaticBitmap中並將其放置在面板上。 – otterb

回答

1

面板尺寸可用後Show()
下面是獲取面板尺寸和適合不同大小的面板相同的圖像的例子。 3個面板顯示相同的圖像。如果您在垂直分割器中顯示圖像,則必須修改Aspect計算。

import wx 

class MyFrame(wx.Frame): 
    def __init__(self, parent, id, title): 
     wx.Frame.__init__(self, parent, id, title, (-1, -1), wx.Size(500, 500)) 
     self.MaxImageSize = 500 
     self.panel1 = wx.Panel(self) 
     self.panel2 = wx.Panel(self) 
     self.panel3 = wx.Panel(self) 
     self.Image1 = wx.StaticBitmap(self.panel1, bitmap=wx.EmptyBitmap(self.MaxImageSize, self.MaxImageSize)) 
     self.Image2 = wx.StaticBitmap(self.panel2, bitmap=wx.EmptyBitmap(self.MaxImageSize, self.MaxImageSize)) 
     self.Image3 = wx.StaticBitmap(self.panel3, bitmap=wx.EmptyBitmap(self.MaxImageSize, self.MaxImageSize)) 

     box = wx.BoxSizer(wx.HORIZONTAL) 
     box.Add(self.panel1, 1,flag = wx.EXPAND|wx.ALL,border = 3) 
     box.Add(self.panel2, 2,flag = wx.EXPAND|wx.ALL,border = 3) 
     box.Add(self.panel3, 3,flag = wx.EXPAND|wx.ALL,border = 3) 
     self.SetSizer(box) 
     Img1 = wx.Image('./image2.png', wx.BITMAP_TYPE_PNG) 
     Img2 = wx.Image('./image2.png', wx.BITMAP_TYPE_PNG) 
     Img3 = wx.Image('./image2.png', wx.BITMAP_TYPE_PNG) 
     self.Show() 
     W,H = self.panel1.GetSize() 
     IW = Img1.GetWidth() 
     IH = Img1.GetHeight() 
     NewW = W 
     if IH>IW: 
      Aspect = float(IW) /float(IH) 
     else: 
      Aspect = float(IH)/float(IW) 
     NewH = W * Aspect 

# scale the image, keeping ratio 
     Img1 = Img1.Scale(NewW,NewH) 
     self.Image1.SetBitmap(wx.BitmapFromImage(Img1)) 

     W,H = self.panel2.GetSize() 
     NewW = W 
     NewH = W * Aspect 
# scale the image, keeping ratio 
     Img2 = Img2.Scale(NewW,NewH) 
     self.Image2.SetBitmap(wx.BitmapFromImage(Img2)) 

     W,H = self.panel3.GetSize() 
     NewW = W 
     NewH = W * Aspect 
# scale the image, keeping ratio 
     Img3 = Img3.Scale(NewW,NewH) 
     self.Image3.SetBitmap(wx.BitmapFromImage(Img3)) 
     self.Fit() 
     self.Layout() 

class MyApp(wx.App): 
    def OnInit(self): 
     frame = MyFrame(None, -1, 'Fit images') 
     frame.Show(True) 
     return True 

app = MyApp(0) 
app.MainLoop()