PyGTK剪贴板set_text的效果仅在进程运行时才持续存在 [英] Effect of PyGTK clipboard set_text persists only while process is running

查看:112
本文介绍了PyGTK剪贴板set_text的效果仅在进程运行时才持续存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本 clip-test.py

#!/usr/bin/python

import gtk

gtk.Clipboard().set_text("exciting clipboard contents")
gtk.Clipboard().set_can_store(None) # not sure if these last two lines are useful
gtk.Clipboard().store()

当我这样运行时:

python clip-test.py

它不起作用。剪贴板未更改。我们可以通过以交互方式运行它而不让进程结束来阐明问题:

it doesn't work. The clipboard is unchanged. We can elucidate the issue by running it in interactive mode and not letting the process finish:

python -i clip-test.py

在退出交互模式之前,请尝试粘贴到某个地方。剪贴板已更改;有用。关闭python之后,剪贴板返回到之前的状态。

Before leaving interactive mode, try pasting somewhere. The clipboard is changed; it works. After closing python, the clipboard returns to what it was before.

一个想法是,这与显示选择剪贴板是默认访问的代码,但是我尝试更改这些参数,但仍然无法正常工作。

One thought was that this has something to do with which display and selection clipboard the code is accessing by default, but I've tried varying those parameters and I still can't get it to work.

我正在使用Python 2.7.3和python-gtk2 2.24.0-3build1,运行Kubuntu 13.04。

I'm using Python 2.7.3 and python-gtk2 2.24.0-3build1, running Kubuntu 13.04.

有什么用?

推荐答案

这不是GTK或PyGTK问题,而是X11复制和粘贴工作原理的结果。在X11应用程序中按 ^ C 时,您尚未真正复制任何内容,只是指示应用程序记住数据以备后用。 仅当粘贴启动时才将数据传输到粘贴程序。这样可以节省资源,并使复印机和粘贴程序可以协商最适合它们的数据传输格式。 Jamie Zawinski的经典文字中对此进行了详细说明。 。

This is not a GTK or PyGTK issue, but a consequence of how X11 copying and pasting works. When you press ^C in an X11 application, you haven't really copied anything yet, you have just instructed the application to remember the data for possible later use. Only when a "paste" is initiated does the data get transmitted to the paster. This saves resources and enables the copier and the paster to negotiate a data transfer format that best suits them. This is explained in some detail in the classic text by Jamie Zawinski.

现代桌面环境请尝试按住到剪贴板的内容,但是您必须进入主循环并在其中停留足够长的时间,以便剪贴板管理器可以捕获剪贴板的内容:

Modern desktop environments do attempt to hold on to the clipboard contents, but you must enter the main loop and remain in it for long enough for the clipboard manager to grab your clipboard contents:

#!/usr/bin/python

import gtk, gobject

gtk.Clipboard().set_text("exciting clipboard contents")
gobject.timeout_add(100, gtk.main_quit)
gtk.main()

set_can_store() store()应该可以加快此过程,一些谷歌搜索显示程序正在使用它们来将剪贴板数据保存为销毁处理程序。目前尚不清楚为什么它对您不起作用-也许您应该调用 set_can_store([( UTF8_STRING,0,0)])而不是 set_can_store(无)

The set_can_store() and store() should be able to speed up the process, some googling showed that programs are using them to save the clipboard data in a destroy handler. It's not clear why it doesn't work for you — perhaps you should be calling set_can_store([("UTF8_STRING", 0, 0)]) instead of set_can_store(None).

这篇关于PyGTK剪贴板set_text的效果仅在进程运行时才持续存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆