使用wxpython实现的一个简单图片浏览器实例

2019-10-06 11:49:22丽君

        wx.Frame.__init__(self, None, -1, "显示图片", size=(400,400))#, style=wx.SIMPLE_BORDER)

        #是否要移动图片的标志
        self.bmoved = False
       
        self.app = app

        #staticbitmap
        self.bmp = wx.StaticBitmap(self, 0, wx.NullBitmap, (0,0), (400,400))


        self.Bind(wx.EVT_MOUSEWHEEL, self.OnChangeImage)
        self.bmp.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
        self.bmp.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
        self.bmp.Bind(wx.EVT_MOTION, self.OnMotion)
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
       
        self.ShowFullScreen(True, style=wx.FULLSCREEN_ALL)
        self.Hide()


    def ShowImage(self, path):
        if os.path.splitext(path)[-1]!='.jpg':
            return
        self.bmppath = path
        image = wx.Image(path, wx.BITMAP_TYPE_JPEG)
        bmp = image.ConvertToBitmap()
        size = self.GetSize(bmp)
        bmp = image.Scale(size[0], size[1]).ConvertToBitmap()
        self.bmp.SetSize(size)
        self.bmp.SetBitmap(bmp)
        self.Show()

    def GetSize(self, bmp):
        width = bmp.GetWidth()
        height = bmp.GetHeight()
        if width>self.max_width:
            height = height*self.max_width/width
            width = self.max_width
        if height>self.max_height:
            width = width*self.max_height/height
            height = self.max_height
        size = width, height
        return size