Iv'e最近开始学习python编程,在我的第一个程序中遇到了一些问题。这是一个自动保存打印屏幕的程序。
如果我在剪贴板中保存了一个打印屏幕并启动程序,它会输出一个.png文件。如果我在剪贴板中没有任何内容的情况下启动程序,然后按print screen,它会输出一个.png文件。
但是,如果我按print screen后,程序已经打印了一个.png文件,它绝对不做任何事情。甚至不能使用ctrl+c复制文本。
这是我使用的代码。
from PIL import ImageGrab
from Tkinter import Tk
import time
r = Tk()
while True:
try:
im = ImageGrab.grabclipboard()
date = time.strftime("%Y-%m-%d %H.%M.%S")
im.save(date + ".png")
r.clipboard_clear()
except IOError:
pass
except AttributeError:
pass发布于 2013-07-24 21:32:57
两点:
while True:)。当你创建自己的主循环时,你会阻止Tkinter做它应该做的处理。你实际上想要做的是更多类似的事情:
import Tkinter as tk
from PIL import Image, ImageGrab
root = tk.Tk()
last_image = None
def grab_it():
global last_image
im = ImageGrab.grabclipboard()
# Only want to save images if its a new image and is actually an image.
# Not sure if you can compare Images this way, though - check the PIL docs.
if im != last_image and isinstance(im, Image):
last_image = im
im.save('filename goes here')
# This will inject your function call into Tkinter's mainloop.
root.after(100, grab_it)
grab_it() # Starts off the process发布于 2013-07-24 20:57:46
如果要为屏幕拍摄图像,则应使用grab()
from PIL import ImageGrab
im = ImageGrab.grab()
im.save("save.png")https://stackoverflow.com/questions/17832717
复制相似问题