我想将图像放入wxPanel的中心,我正在考虑我需要调整图像大小以适应wxPanel。但是我无法动态地获得wxPanel的大小。这里是一个测试代码
#!/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 :(得分: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()