Running Python 3.4 on Windows 7.
I need to copy what's stored in the clipboard to a variable in my python program. I've seen on Stack Overflow that that can be done either with pywin32
or tkinter
. Since tkinter
is part of the python standard library, I decided that that was the better of the two since the user won't have to install an external module. Here's the code for getting the clipboard data in tkinter
:
import tkinter
number = tkinter.Tk().clipboard_get()
This works fine except a blank tkinter
window pops up every time this executes.
Why is this happening? Normally tkinter
doesn't display anything until tk().mainloop()
is run.
Is there any way to avoid this window popping up? If not, I guess I'll just use pywin32
.
However, to hide the Tkinter window, we generally use the “withdraw” method that can be invoked on the root window or the main window. In this example, we have created a text widget and a button “Quit” that will close the root window immediately.
Import the tkinter library and create an instance of tkinter frame. Set the size of the frame using geometry method. Next, call clipboard_get() to get the text from the clipboard and store the data in a variable "cliptext". Create a label to the display the clipboard text.
You can use the module called win32clipboard, which is part of pywin32. An important reminder from the documentation: When the window has finished examining or changing the clipboard, close the clipboard by calling CloseClipboard. This enables other windows to access the clipboard.
This works fine except a blank tkinter window pops up every time this executes.
You can hide this window:
from tkinter import Tk
root = Tk()
root.withdraw()
number = root.clipboard_get()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With