无法附加到剪贴板

发布于 2025-01-07 04:39:06 字数 296 浏览 1 评论 0原文

每当我在 python 解释器中尝试以下操作时。我能够复制“你好”这个词 到命令行,即使在我关闭解释器之后

from Tkinter import Tk
r = Tk()
r.clipboard_append(" hello ")

但是如果我将其放入名为 test.py 的文件中然后尝试

python test.py

这将不起作用,我无法将其附加到系统剪贴板

有谁知道为什么不或知道什么在脚本和解释器中运行它之间的差异会导致

Whenever I try the following in my python interpreter. I am able to copy the word hello
to the command line, even after I close the interpreter

from Tkinter import Tk
r = Tk()
r.clipboard_append(" hello ")

However if I put this in a file called test.py and then try

python test.py

This will not work, I can't append this to the system clipboard

Does any one know why not or know what difference between running it in a script and in the interpreter would cause

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

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

发布评论

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

评论(3

指尖上得阳光 2025-01-14 04:39:06

显然,在 Tkinter 进入主循环之前它不会起作用。这适用于我的系统:

from Tkinter import *
r = Tk()
r.clipboard_append("hello")
r.mainloop()

Apparently it won't work until Tkinter is in it's mainloop. This works on my system:

from Tkinter import *
r = Tk()
r.clipboard_append("hello")
r.mainloop()
好倦 2025-01-14 04:39:06

我也看到了行为上的这种差异。建议的用于将文本放置在剪贴板上的 tkinter 解决方案可以通过命令行正常工作,但在程序中使用时会将剪贴板留空。在程序末尾使用 mainloop() 可以工作,但意味着程序不会结束,而使用 r.update() 似乎没有帮助。

注意:如果在程序结束之前将剪贴板粘贴到另一个应用程序(通过使程序挂在末尾等待用户输入),则 tkinter 解决方案可以正常工作,即使在程序结束之后也是如此。但是,如果程序在剪贴板粘贴到另一个程序之前结束,则剪贴板最终会变成空的。

I see this difference in behavior too. The suggested tkinter solution for placing text on the clipboard works fine via the command line, but leaves the clipboard empty when used in a program. Using mainloop() at the end of the program works but means the program won't end, and using r.update() doesn't seem to help.

Note: If the clipboard is pasted to another application before the program ends (by making the program hang at the end waiting for user input), then the tkinter solution works fine, even after the program ends. But if the program ends before the clipboard is pasted to another program, the clipboard ends up being empty.

我恋#小黄人 2025-01-14 04:39:06

与 deel 一样,Patrick T Neslon 和其他人建议使用:

from Tkinter import *
r = Tk()
r.clipboard_append("hello")
r.mainloop()

在非 GUI 应用程序中不起作用(更新也不起作用)(Win 7/64,Python 2.7.10/32)。尽管它似乎是一种黑客行为,但它确实有效,灵感来自 https://bugs.python.org/issue23760

from Tkinter import *
r = Tk()
r.clipboard_clear()
r.clipboard_append("hello")
r.after(500, tk.destroy)
r.mainloop()

我的系统通常需要 100 毫秒,所以使用 500 毫秒。这是我将要使用的实用程序,不是我想要广泛分发的东西,除非有更多经验的人说它可靠......

Like deel, the suggestion of Patrick T Neslon and others to use:

from Tkinter import *
r = Tk()
r.clipboard_append("hello")
r.mainloop()

didn't work in a non-gui application (update didn't work either) (Win 7/64, Python 2.7.10/32). This works though it seems to be a hack, inspired by https://bugs.python.org/issue23760:

from Tkinter import *
r = Tk()
r.clipboard_clear()
r.clipboard_append("hello")
r.after(500, tk.destroy)
r.mainloop()

100 ms usually worked on my system so went with 500. This for a utility that I will be using, not something I'd want to distribute very widely unless somebody with much more experience says its reliable...

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