Python 如何访问 X11 剪贴板?

发布于 2024-09-27 05:20:53 字数 221 浏览 2 评论 0原文

我希望我的 Python 脚本能够通过 x11 复制并粘贴到剪贴板(以便它可以在 Linux 上运行)。谁能给我指出我可以查看的具体资源,或者我必须掌握的概念?

这可以通过 http://python-xlib.sourceforge.net 处的 Python X 库来实现吗?

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). 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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

日暮斜阳 2024-10-04 05:20:53

Cameron Laird 的回答中提到的基于 Tkinter 的解决方案

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

将“CLIPBOARD”替换为“PRIMARY”以改为获取 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-based solution mentioned in Cameron Laird's answer:

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

Replace "CLIPBOARD" with "PRIMARY" to get PRIMARY selection instead.

Also see this answer.

python-xlib solution, based on PrintSelection() and 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") )
相权↑美人 2024-10-04 05:20:53

我更喜欢基于 Tkinter 的解决方案,而不是需要 pygtk 的解决方案,仅仅是因为后者可能面临安装挑战。鉴于此,我对阿尔文·史密斯的建议是阅读:剪切&在 Tkinter 小部件之间粘贴文本

您可以在 Tkinter 事件处理程序中使用此代码(来自 python-list 通过 Tkinter 剪贴板访问):

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

I favor a Tkinter-based solution over one which requires pygtk, simply because of the potential the latter has for installation challenges. Given this, my recommendation to Alvin Smith is to read: Cut & Paste Text Between Tkinter Widgets

You can use this code in a Tkinter event handler (from python-list via Tkinter Clipboard access):

data =  event.widget.selection_get(selection="CLIPBOARD"))
沉溺在你眼里的海 2024-10-04 05:20:53

您可以使用 pygtk 来完成此操作。一个干净的解决方案,但可能有点矫枉过正,具体取决于您的应用程序。

另一种方法可以获取一些 google-hits 是对 < a href="http://www.kfish.org/software/xsel/xsel.1x.html" rel="nofollow">xsel。

You can do this with 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.

空气里的味道 2024-10-04 05:20:53

使用 clipboard 模块

首先,安装 clipboard使用 pip3模块:

$ sudo pip3 install clipboard

使用这个跨平台模块(Linux、Mac、Windows)非常简单:

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

Using the clipboard module

First, install the clipboard module using pip3:

$ sudo pip3 install clipboard

Using this cross-platform module (Linux, Mac, Windows) is pretty straightforward:

import clipboard
clipboard.copy('text')   # Copy to the clipboard.
text = clipboard.paste()   # Copy from the clipboard.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文