使用 tkinter 从剪贴板复制而不显示窗口 [英] Copying from clipboard using tkinter without displaying window
问题描述
在 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.
为什么会这样?通常
tkinter
在tk().mainloop()
运行之前不会显示任何内容.
Why is this happening? Normally
tkinter
doesn't display anything untiltk().mainloop()
is run.
有什么办法可以避免弹出这个窗口?如果没有,我想我只会使用 pywin32
.
Is there any way to avoid this window popping up? If not, I guess I'll just use pywin32
.
推荐答案
窗口由 tkinter.Tk()
(或其他需要窗口的元素)创建,而不是由 tk() 创建.主循环()
.Mainloop 保持程序运行.
Window is created by tkinter.Tk()
(or other elements which need window) not by tk().mainloop()
. Mainloop keeps program working.
这篇关于使用 tkinter 从剪贴板复制而不显示窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!