Reading an image from the clipboard with wxPython [closed]

放肆的年华 提交于 2019-12-12 22:19:37

问题


How can I read an image from the clipboard? I'm able to read text from the clipboard using wx.Clipboard, but not images.

Is it possible to read images with wx.Clipboard? If not, is there another way?

I'm using Python 2.5 and Windows Vista 64-bit.


回答1:


The following works for me (tested on Mac OSX)

import wx
class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'test frame',size=(790, 524))
        self.Bind(wx.EVT_LEFT_DOWN, self.OnClick)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.clip = wx.Clipboard()
        self.x = wx.BitmapDataObject()
        self.bmp = None

    def OnClick(self, evt):
        self.clip.Open()
        self.clip.GetData(self.x)
        self.clip.Close()
        self.bmp = self.x.GetBitmap()
        self.Refresh()

    def OnPaint(self, evt):
        if self.bmp:
            dc = wx.PaintDC(self)
            dc.DrawBitmap(self.bmp, 20, 20, True)

if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame()
    frame.Show()
    app.MainLoop()

To use this, I run it and when the frame comes up I copy an image using another program and then click in the wx frame, which then causes the copied image to be drawn within it.




回答2:


Python Imaging Library has an ImageGrab module that can do just that. This works on windows only.



来源:https://stackoverflow.com/questions/2629907/reading-an-image-from-the-clipboard-with-wxpython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!