2

我正在开发可以生成图表的 PyGTK 应用程序。这是通过 matplotlib 实现的。如何添加复制到剪贴板功能?如何将图形复制到剪贴板?

4

1 回答 1

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()
于 2012-05-17T13:50:43.083 回答