2010-10-10 14 views

Antwort

3

Ich bevorzuge eine Tkinter-basierte Lösung gegenüber einer, die pygtk erfordert, einfach wegen des Potenzials, das letzteres für Installationsherausforderungen hat. Angesichts dieser Tatsache ist meine Empfehlung an Alvin Smith ist zu lesen:

+0

Der erste Link ist kaputt. –

1

Tkinter-basierte Lösung erwähnt in Cameron Laird's answer:

import Tkinter 
root = Tkinter.Tk() 
print(root.selection_get(selection="CLIPBOARD")) 

Ersetzen Sie "Zwischenablage" mit "PRIMARY", um stattdessen PRIMARY Auswahl zu erhalten.

Siehe auch this answer.

python-xlib Lösung, basierend auf PrintSelection() und python-xlib/examples/get_selection.py

from Xlib import X, display as Xdisplay 

def property2str(display, prop): 
    if prop.property_type == display.get_atom("STRING"): 
     return prop.value.decode('ISO-8859-1') 
    elif prop.property_type == display.get_atom("UTF8_STRING"): 
     return prop.value.decode('UTF-8') 
    else: 
     return "".join(str(c) for c in prop.value) 

def get_selection(display, window, bufname, typename): 
    bufid = display.get_atom(bufname) 
    typeid = display.get_atom(typename) 
    propid = display.get_atom('XSEL_DATA') 
    incrid = display.get_atom('INCR') 

    window.change_attributes(event_mask = X.PropertyChangeMask) 
    window.convert_selection(bufid, typeid, propid, X.CurrentTime) 
    while True: 
     ev = display.next_event() 
     if ev.type == X.SelectionNotify and ev.selection == bufid: 
      break 

    if ev.property == X.NONE: 
     return None # request failed, e.g. owner can't convert to target format type 
    else: 
     prop = window.get_property(propid, X.AnyPropertyType, 0, 2**31-1, 1) 

     if prop.property_type == incrid: 
      result = "" 
      while True: 
       while True: 
        ev = display.next_event() 
        if ev.type == X.PropertyNotify and ev.atom == propid and ev.state == X.PropertyNewValue: 
         break 

       prop = window.get_property(propid, X.AnyPropertyType, 0, 2**31-1, 1) 
       if len(prop.value) == 0: 
        break 

       result += property2str(display, prop) 
      return result 
     else: 
      return property2str(display, prop) 

display = Xdisplay.Display() 
window = display.screen().root.create_window(0,0, 1,1, 0, X.CopyFromParent) 
print(get_selection(display, window, "CLIPBOARD", "UTF8_STRING") or \ 
     get_selection(display, window, "CLIPBOARD", "STRING")) 
0

Mit dem clipboard Modul

Installieren Sie zuerst den clipboard Modul pip3 mit:

$ sudo pip3 install clipboard 

Mit dieser Quer -Plattformmodul (Linux, Mac, Windows) ist ziemlich einfach:

import clipboard 
clipboard.copy('text') # Copy to the clipboard. 
text = clipboard.paste() # Copy from the clipboard.