How to copy to clipboard with X11?

前端 未结 2 1584
被撕碎了的回忆
被撕碎了的回忆 2021-01-04 22:27

Using the frameworks on OS X, I can use the following to copy a PNG to the pasteboard (in C — obviously I could use NSPasteboard with Cocoa):

#include 

        
2条回答
  •  我在风中等你
    2021-01-04 22:53

    The ability to store data on the GTK clipboard after a program terminates is not well supported. GTK.clipboard.store may fail to store larger images (greater than several hundred kB), and advanced desktop features like compiz may conflict with this mechanism. One solution without these drawbacks is to run a simple gtk application in the background. The following Python server application uses the Pyro package to expose the methods of ImageToClipboard:

    
    #! /usr/bin/env python
    # gclipboard-imaged.py
    import gtk, sys, threading;
    import Pyro.core;
    
    class ImageToClipboard(Pyro.core.ObjBase):
       def __init__(self, daemon):
          Pyro.core.ObjBase.__init__(self)
          self.daemon = daemon;
       def _set_image(self, img):
          clp = gtk.clipboard_get();
          clp.set_image(img);
       def set_image_from_filename(self, filename):
          with gtk.gdk.lock:
             img = gtk.gdk.pixbuf_new_from_file(filename);
             self._set_image(img);
       def quit(self):
          with gtk.gdk.lock:
             gtk.main_quit();
          self.daemon.shutdown();
    
    class gtkThread( threading.Thread ):
       def run(self):
          gtk.main();
    
    def main():
       gtk.gdk.threads_init();
       gtkThread().start();
       Pyro.core.initServer();
       daemon = Pyro.core.Daemon();
       uri = daemon.connect(ImageToClipboard(daemon),"imagetoclipboard")
       print "The daemon running on port:",daemon.port
       print "The object's uri is:",uri
       daemon.requestLoop();
       print "Shutting down."
       return 0;
    
    if __name__=="__main__":
       sys.exit( main() )
    

    Start this program as a background process, i.e.

    gclipboard-imaged.py &

    The following example client application sets the clipboard image using a filename given at the command line:

    
    #! /usr/bin/env python
    # gclipboard-setimage.py
    import Pyro.core, sys;
    
    serverobj =  Pyro.core.getProxyForURI("PYROLOC://localhost:7766/imagetoclipboard");
    filename = sys.argv[1];
    serverobj.set_image_from_filename(filename);
    

    To copy an image to the clipboard, run

    gclipboard-setimage.py picname.png

提交回复
热议问题