问题描述
在Windows 7上运行Python 3.4。
Running Python 3.4 on Windows 7.
我需要将剪贴板中存储的内容复制到python程序中的变量中。我在Stack Overflow上看到可以用pywin32或tkinter来完成。由于tkinter是python标准库的一部分,因此我认为这是两者中最好的,因为用户无需安装外部模块。这是在tkinter中获取剪贴板数据的代码:
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()
这很好,除了每次执行时都会弹出一个空白的tkinter窗口。
This works fine except a blank tkinter window pops up every time this executes.
1)为什么会发生这种情况?通常,tkinter在运行tk()。mainloop()之前不会显示任何内容。
1) Why is this happening? Normally tkinter doesn't display anything until tk().mainloop() is run.
2)有什么办法可以避免此窗口弹出?如果没有,我想我只会用pywin32
2) Is there any way to avoid this window popping up? If not, I guess I'll just use pywin32
推荐答案
窗口是由 tkinter.Tk( )
(或其他需要窗口的元素),而不是 tk()。mainloop()
。 Mainloop保持程序正常运行。
Window is created by tkinter.Tk()
(or other elements which need window) not by tk().mainloop()
. Mainloop keeps program working.
也许尝试或
这篇关于Python:使用tkinter从剪贴板复制而不显示窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!