Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
355 views
in Technique[技术] by (71.8m points)

python - Copying from clipboard using tkinter without displaying window

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...