如何在滚动面板中显示图像

时间:2014-01-03 09:38:59

标签: python wxpython

我只知道如何在普通面板中显示图像。 如何在滚动面板中显示。图像可以改变。我在谷歌搜索过。但我无法在我的框架中获得合适的代码 以下是我在普通面板中显示图像的代码

#!/usr/bin/env python

import numpy 
import sys, os
import wx
import wx.lib.scrolledpanel as scrolled
from PIL import Image



class  WinFrame(wx.Frame):
    def __init__(self, parent, title, width, height):
        super(WinFrame, self).__init__(parent, 
                                       title=title,
                                       size=(width, height))
        self.Panel = wx.Panel(self,size=(width/4,height),
                                  pos=(0,0),
                                  style=wx.BORDER)        

        self.imgPanel = wx.Panel(parent=self,
                                 size=(width, height),
                                 pos=(0,0),
                                 style=wx.BORDER)

        self.bitMap = wx.StaticBitmap(parent=self.imgPanel)

        img = Image.open('./image.jpg')
        img=img.resize([1280, 1280], Image.ANTIALIAS)

        wxImg = wx.EmptyImage(img.size[0], img.size[1])
        wxImg.SetData(img.tostring())
        wxImg = wxImg.ConvertToBitmap()
        self.bitMap.SetBitmap(wxImg)

        self.Centre()
        self.Show(True)        

    def __del__(self):
        super(WinFrame, self).__del__()

class myApp(wx.App):
    def __init__(self, width, height):
        super(myApp, self).__init__(0)

        self.width = width
        self.height = height

    def createFrame(self):
        self.frame = WinFrame(None, "show_img", self.width, self.height)
        self.SetTopWindow(self.frame)    

    def __del__(self):
        super(myApp, self).__del__()


def main():
    app = myApp(480, 480)
    app.createFrame()
    app.MainLoop()

if "__main__" == __name__ :
    main()

1 个答案:

答案 0 :(得分:1)

使用滚动面板。并添加静态位图。喜欢以下代码

#!/usr/bin/env python

import sys, os
import wx
import wx.lib.scrolledpanel as scrolled

class ImgPanel(scrolled.ScrolledPanel):
    def __init__(self, parent):
        super(ImgPanel, self).__init__(parent, 
                                       style = wx.SUNKEN_BORDER)

        self.bitmap=wx.StaticBitmap(parent=self)
        image = wx.Bitmap('image.jpg')
        self.bitmap.SetBitmap(image)

        self.imgSizer = wx.BoxSizer(wx.VERTICAL)        
        self.imgSizer.Add(self.bitmap, 1, wx.EXPAND)
        self.SetSizer(self.imgSizer)

        self.SetAutoLayout(1)
        self.SetupScrolling()    
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.bitmap.Bind(wx.EVT_MOTION, self.OnMove)
        self.bitmap.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        self.bitmap.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
        self.IsRectReady = False
        self.newRectPara=[0,0,0,0]

class  WinFrame(wx.Frame):
    def __init__(self, parent, title, width, height):
        super(WinFrame, self).__init__(parent, 
                                       title=title,
                                       size=(width, height))

        self.imgPanel = ImgPanel(self)
        self.frameSizer = wx.BoxSizer(wx.HORIZONTAL)        
        self.frameSizer.Add(self.imgPanel, 1, wx.EXPAND)        
        self.SetAutoLayout(True)
        self.SetSizer(self.frameSizer)
        self.Layout()      

        self.Centre()
        self.Show(True)        


class MyApp(wx.App):
    def __init__(self, width, height):
        super(MyApp, self).__init__(0)

        self.width = width
        self.height = height

    def createFrame(self):
        self.frame = WinFrame(None, "test", self.width, self.height)
        self.SetTopWindow(self.frame)    

def main():
    app = MyApp(640, 480)
    app.createFrame()
    app.MainLoop()

if "__main__" == __name__ :
    main()