简体   繁体   English

Python 如何访问 X11 剪贴板?

[英]How can Python access the X11 clipboard?

I want my Python script to be able to copy and paste to/from the clipboard via x11 (so that it will work on Linux).我希望我的 Python 脚本能够通过 x11 向/从剪贴板复制和粘贴(以便它可以在 Linux 上运行)。 Can anyone point me to specific resources I can look at, or the concepts I would have to master?谁能指出我可以查看的特定资源,或者我必须掌握的概念?

Is this possible to do with the Python X library at http://python-xlib.sourceforge.net ?这可能与http://python-xlib.sourceforge.net上的 Python X 库有关吗?

Tkinter-based solution mentioned in Cameron Laird's answer : Cameron Laird 的回答中提到的基于 Tkinter 的解决方案

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

Replace "CLIPBOARD" with "PRIMARY" to get PRIMARY selection instead.用“PRIMARY”替换“CLIPBOARD”以获得PRIMARY选择。

Also see this answer .另请参阅此答案

python-xlib solution , based on PrintSelection() and python-xlib/examples/get_selection.py 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") )

I favor a Tkinter-based solution over one which requires pygtk, simply because of the potential the latter has for installation challenges.我更喜欢基于 Tkinter 的解决方案,而不是需要 pygtk 的解决方案,这仅仅是因为后者具有安装挑战的潜力。 Given this, my recommendation to Alvin Smith is to read: Cut & Paste Text Between Tkinter Widgets鉴于此,我对 Alvin Smith 的建议是阅读: 在 Tkinter 小部件之间剪切和粘贴文本

You can use this code in a Tkinter event handler (from python-list via Tkinter Clipboard access ):您可以在 Tkinter 事件处理程序中使用此代码(从python-list通过Tkinter Clipboard access ):

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

You can do this with pygtk . 你可以用 pygtk 做到这一点 A clean solution but might be a bit overkill depending on your application.一个干净的解决方案,但根据您的应用程序可能有点矫枉过正。

Another method that gets some google - hits is to make a system call to xsel .获得一些谷歌点击量的另一种方法是对xsel进行系统调用。

您可能会发现此线程很有用: X11 剪贴板如何处理多种数据格式?

Using the clipboard module使用clipboard模块

First, install the clipboard module using pip3 :首先,使用pip3安装clipboard模块:

$ sudo pip3 install clipboard

Using this cross-platform module (Linux, Mac, Windows) is pretty straightforward:使用这个跨平台模块(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