I'm working on PyGTK application, that can generate graphs. This achieved by matplotlib. How can I add Copy-To-Clipboard functionality? How to copy figure to clipboard?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
This will do it in Linux (just mouse-click on the image and it is copied to clipboard ready to paste in e.g. GIMP):
import matplotlib.pyplot as plt
from matplotlib.backends.backend_gtk import FigureCanvasGTK as FigureCanvas
import gtk
import numpy as np
class W(gtk.Window):
def __init__(self):
gtk.Window.__init__(self)
img = np.random.uniform(0,1,(200,200))
fig = plt.Figure()
fig.gca().imshow(img)
self.image_canvas = FigureCanvas(fig)
self.image_canvas.connect('button_press_event', self.do_clip)
self.add(self.image_canvas)
self.show_all()
def do_clip(self, widget=None, event=None):
snap = self.image_canvas.get_snapshot()
pixbuf = gtk.gdk.pixbuf_get_from_drawable(None, snap,
snap.get_colormap(),0,0,0,0,
snap.get_size()[0], snap.get_size()[1])
clip = gtk.Clipboard()
clip.set_image(pixbuf)
w=W()
gtk.main()