簡體   English   中英

Python 如何訪問 X11 剪貼板?

[英]How can Python access the X11 clipboard?

我希望我的 Python 腳本能夠通過 x11 向/從剪貼板復制和粘貼(以便它可以在 Linux 上運行)。 誰能指出我可以查看的特定資源,或者我必須掌握的概念?

這可能與http://python-xlib.sourceforge.net上的 Python X 庫有關嗎?

Cameron Laird 的回答中提到的基於 Tkinter 的解決方案

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

用“PRIMARY”替換“CLIPBOARD”以獲得PRIMARY選擇。

另請參閱此答案

python-xlib 解決方案,基於PrintSelection()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") )

我更喜歡基於 Tkinter 的解決方案,而不是需要 pygtk 的解決方案,這僅僅是因為后者具有安裝挑戰的潛力。 鑒於此,我對 Alvin Smith 的建議是閱讀: 在 Tkinter 小部件之間剪切和粘貼文本

您可以在 Tkinter 事件處理程序中使用此代碼(從python-list通過Tkinter Clipboard access ):

data =  event.widget.selection_get(selection="CLIPBOARD"))

你可以用 pygtk 做到這一點 一個干凈的解決方案,但根據您的應用程序可能有點矯枉過正。

獲得一些谷歌點擊量的另一種方法是對xsel進行系統調用。

您可能會發現此線程很有用: X11 剪貼板如何處理多種數據格式?

使用clipboard模塊

首先,使用pip3安裝clipboard模塊:

$ sudo pip3 install clipboard

使用這個跨平台模塊(Linux、Mac、Windows)非常簡單:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM