我正在研究可以生成图形的PyGTK应用程序。这是通过matplotlib实现的。如何添加“复制到剪贴板”功能?如何将图形复制到剪贴板?
答案 0 :(得分:3)
这将在Linux中完成(只需在图像上单击鼠标即可将其复制到剪贴板中,以便粘贴到例如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()