利用 Python 和 Tkinter 从剪贴板中复制


我们可以使用 Tkinter 的 clipboard_get() 方法从剪贴板中进行复制。我们举个例子,看看如何从剪贴板获取数据并将其显示在 Tkinter 窗口上。

步骤 −

  • 导入 Tkinter 库并创建一个 Tkinter 框架实例。

  • 使用 geometry 方法设置框架大小。

  • 接下来,调用 clipboard_get() 从剪贴板中获取文本,并将数据存储在变量 “cliptext” 中。

  • 创建一个标签以显示剪贴板文本。传递 cliptext 作为 text“text=cliptext”

  • 最后,运行应用程序窗口的 mainloop

示例

# Import the tkinter library from tkinter import * # Instance of tkinter canvas win = Tk() win.geometry("700x250") win.title("Data from Clipboard") # Get the data from the clipboard cliptext = win.clipboard_get() # Label to print clipboard text lab=Label(win, text = cliptext, font=("Calibri",15,"bold")) lab.pack(padx=20, pady=50) # Run the mainloop win.mainloop()

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

输出

它将产生以下输出 −

它将在窗口上显示剪贴板的内容。

更新于: 26-10-2021

6K+ 的浏览量

启动你的职业

通过完成课程获得认证

开始吧
广告