wxpython->图片显示不完整
wxpython->图片显示不完整
主要是运行后显示的图片仿佛边缘被裁减过的样子。谁能解释一下原因?
#!/usr/bin/env python
"""Hello, wxPython! program."""
import wx
class Frame(wx.Frame):
"""Frame class that displays an image."""
def __init__(self, image):
"""Create a Frame instance and display image."""
temp = image.ConvertToBitmap()
size = temp.GetWidth(), temp.GetHeight()
wx.Frame.__init__(self, parent=None, id=-1, title='Hello, wxPython!',
pos=wx.DefaultPosition,size=size)
self.panel = wx.Panel(self)
wx.StaticBitmap(parent=self.panel, bitmap=temp)
self.Centre()
class App(wx.App):
"""Application class."""
def OnInit(self):
image = wx.Image('img.jpg', wx.BITMAP_TYPE_JPEG)
self.frame = Frame(image)
self.frame.Show()
self.SetTopWindow(self.frame)
return True
def main():
app = App()
app.MainLoop()
if __name__ == '__main__':
main()
[解决办法]
考虑你有很多控件的时候,自己算很麻烦吧,所以一般是建议采用sizer方式布局方式,让程序自己去计算,譬如你的代码可以改成下面,不用管图片大小,只要求panel占满空间:
def __init__(self, image):
"""Create a Frame instance and display image."""
temp = image.ConvertToBitmap()
#~ size = temp.GetWidth(), temp.GetHeight()
wx.Frame.__init__(self, parent=None, id=-1, title='Hello, wxPython!')#, pos=wx.DefaultPosition,size=size)
#~ pos=wx.DefaultPosition,size=size)
self.panel = wx.Panel(self)
wx.StaticBitmap(parent=self.panel, bitmap=temp)
sizer = wx.BoxSizer()
sizer.Add(self.panel, 1, wx.EXPAND)
self.SetSizerAndFit(sizer)
self.Centre()