您好我是python编程的新手,我试图在按钮点击时截取所选区域的屏幕截图。它在第一次点击时按预期工作,但如果我想捕获多次,则会引发错误。
异常AttributeError:“'HookManager'对象在>中没有属性'keyboard_hook'”忽略
下面是我的代码。class CaptureScreenshot(PyMouseEvent):
app = wx.App()
dc = wx.ScreenDC()
dc.StartDrawingOnTop(None)
dc.SetPen(wx.Pen('green', 2))
dc.SetBrush(wx.TRANSPARENT_BRUSH)
# To draw the crosshair
SetSystemCursor = windll.user32.SetSystemCursor #reference to function
SetSystemCursor.argtype = [c_int, c_int] #arguments
SetSystemCursor.restype = c_int #return
LoadCursorFromFile = windll.user32.LoadCursorFromFileA #reference to function
LoadCursorFromFile.argtype = c_char_p #arguments
LoadCursorFromFile.restype = c_int #return
CrossHeirPointer = "D:/ccrosshair.cur"
NormalPointer = "D:/Arrow.cur"
i = 0
def __init__(self):
PyMouseEvent.__init__(self)
self.logger = Logger(self.__class__.__name__).get()
NewCursor = self.LoadCursorFromFile(self.CrossHeirPointer)
if NewCursor is None:
print "Error loading the cursor"
elif self.SetSystemCursor(NewCursor, win32con.IDC_ARROW) == 0:
print "Error in setting the cursor"
def click(self, x, y, button, press):
if button == 1 and self.i == 0:
global x1,y1,x2,y2
if press:
x1 = x
y1 = y
else:
x2 = x
y2 = y
self.i = self.i + 1
self.dc.DrawRectangle(x1, y1, x2-x1, y2-y1)
im = ImageGrab.grab(bbox=(x1,y1,x2,y2))
time.sleep(0.5)
try:
im.save("path to save the screenshot")
time.sleep(0.5)
except Exception, err:
print "Wrong selection", Exception, err
finally:
self.dc.EndDrawingOnTop()
self.dc.Clear()
NewCursor = self.LoadCursorFromFile(self.NormalPointer)
if NewCursor is None:
print "Error loading the cursor"
elif self.SetSystemCursor(NewCursor, win32con.IDC_ARROW) == 0:
print "Error in setting the cursor"
self.stop()
self = 'close'
else:
self.stop()
class test():
w = tk.Tk()
c = CaptureScreenshot()
def __init__(self):
self.logger = Logger(self.__class__.__name__).get()
self.w.title("Test Window")
self.w.minsize(500, 600)
def capture(self):
self.c.run()
t = test()
b = tk.Button(t.w, text = 'ok', command = t.capture, width = 40).pack()
t.w.mainloop()