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.
1) Why is this happening? Normally tkinter doesn't display anything until tk().mainloop() is run.
2) Is there any way to avoid this window popping up? If not, I guess I'll just use pywin32
Window is created by
tkinter.Tk()
(or other elements which need window) not bytk().mainloop()
. Mainloop keeps program working.Maybe try Pyperclip or clipboard
You can hide this window:
You actually do it without
tkinter
and in a much more simple way:I had the same problem. This worked for me on windows 7, python 2.7. I now only get one window.
Here's a Python function based on this answer that replaces/returns clipboard text using Tkinter, a built-in Python module, without showing the window.
A small disadvantage with using this Tkinter based method is that it uses a quickly hidden window which isn't ideal but this shouldn't be noticeable.
This answer uses content from my original answer on the Stack Overflow question How to copy/get an image in the clipboard with Python (I accept Tkinter for text).