How to copy from clipboard using tkinter without displaying a window

PythonTkinterGUI-Programming

Let us suppose that in a particular application, we have to copy the content residing in the clipboard. We can access the clipboard using clipboard_get().

After copying the text from the clipboard, it will reside in the cache memory through which we can debug the program and display the text in the frame, then we can see the copied text from the clipboard.

First, we will create a window which will store the copied characters or text from the source using the get method. Once the execution is done, then we can hide the window by using the “withdraw” method in tkinter. It helps to get rid of the window.

Example

#Import the tkinter library
from tkinter import *

#Create an instance of tkinter canvas by executing it
win = Tk()
win.geometry("600x200")

#Get the data from the clipboard
cliptext = win.clipboard_get()

#Create the label for the clipboard
lab=Label(win, text = cliptext)
lab.pack()

#Keep Running the window
win.mainloop()

Output

Running the above code snippet will copy the content from the clipboard and display it in a window.

To avoid the window, we can use “withdraw” method,

from tkinter import *
win = Tk()
win.withdraw()
number = win.clipboard_get()
raja
Published on 04-Mar-2021 13:59:30
Advertisements