如何将字符串复制到剪贴板? [英] How do I copy a string to the clipboard?

查看:80
本文介绍了如何将字符串复制到剪贴板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个基本的Windows应用程序,该应用程序使用用户输入来构建字符串,然后将其添加到剪贴板.如何使用Python将字符串复制到剪贴板?

I'm trying to make a basic Windows application that builds a string out of user input and then adds it to the clipboard. How do I copy a string to the clipboard using Python?

推荐答案

实际上, pywin32 ctypes 似乎对这个简单的任务是过大的. Tkinter 是一个跨平台的GUI框架,默认情况下随Python一起提供,并且具有剪贴板访问方法以及其他很酷的东西.

Actually, pywin32 and ctypes seem to be an overkill for this simple task. Tkinter is a cross-platform GUI framework, which ships with Python by default and has clipboard accessing methods along with other cool stuff.

如果您只需要在系统剪贴板中放置一些文本,即可做到:

If all you need is to put some text to system clipboard, this will do it:

from Tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append('i can has clipboardz?')
r.update() # now it stays on the clipboard after the window is closed
r.destroy()

仅此而已,无需弄乱特定于平台的第三方库.

And that's all, no need to mess around with platform-specific third-party libraries.

如果您使用的是Python 3,请用 tkinter 替换 TKinter .

If you are using Python 3, replace TKinter with tkinter.

这篇关于如何将字符串复制到剪贴板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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