# pip install pyscreenshot
import tkinter as tk
import pyscreenshot as ImageGrab
import datetime
class Application(tk.Tk):
def __init__(self):
super().__init__()
self.title("Capture: Enter, Resize: ←↑↓→")
self.geometry("500x300")
self.config(bg="white")
self.attributes("-transparentcolor", "white")
self.attributes("-topmost", True)
self.bind("",self.key_pressed)
def key_pressed(self, event):
#print(event.keysym)
capKeyList = ['p', 'Return']
if(event.keysym in capKeyList ):
self.capture()
if(event.keysym == 'Up'):
newW = self.winfo_width()
newH = self.winfo_height() - 10
self.geometry(str(newW)+"x"+str(newH))
if(event.keysym == 'Down'):
newW = self.winfo_width()
newH = self.winfo_height() + 10
self.geometry(str(newW)+"x"+str(newH))
if(event.keysym == 'Left'):
newW = self.winfo_width() -10
newH = self.winfo_height()
self.geometry(str(newW)+"x"+str(newH))
if(event.keysym == 'Right'):
newW = self.winfo_width() +10
newH = self.winfo_height()
self.geometry(str(newW)+"x"+str(newH))
#self.pressed[event.keysym] = True
def capture(self):
canvas = self
box = ( canvas.winfo_rootx(),
canvas.winfo_rooty(),
canvas.winfo_rootx() + canvas.winfo_width(),
canvas.winfo_rooty() + canvas.winfo_height())
grab = ImageGrab.grab(bbox = box)
now = datetime.datetime.now()
filename = './cap_' + now.strftime('%Y%m%d_%H%M%S')
grab.save(filename+".png")
print('saved: '+filename+".png")
self.title("Saved: "+filename+".png")
if __name__ == "__main__":
application = Application()
application.mainloop()
最終更新:2021年02月05日 19:31