#! /Usr/bin/python ''' By zevolo, 2012.12.20 ''' Import gtk. gdk Import gtk Import glib Class MyRect (): Def _ init _ (self, x = 0, y = 0, w = 0, h = 0 ): Self. x = x Self. y = y Self. w = w Self. h = h Def _ init _ (self, x, y ): Self. x = min (int (x. x), int (y. x )) Self. y = min (int (x. y), int (y. y )) Self. w = abs (int (y. x-x. x )) Self. h = abs (int (y. y-x. y )) Class MyPair (): Def _ init _ (self, x = 0, y = 0 ): Self. x = x Self. y = y Class MyPoint (MyPair ): Def _ init _ (self, x = 0, y = 0 ): MyPair. _ init _ (self, x, y) Class MySize (MyPair ): Def _ init _ (self, w = 0, h = 0 ): MyPair. _ init _ (self, x, y) Class MyCapture (): (Event_enter, event_leave) = (0, 1) Def _ init _ (self ): Pass Def capture (self ): Pass Def handleEvent (self, event ): If event = event_enter: EnterSnap () Elif event = event_leave: LeaveSnap () Def enterSnap (self ): Pass Def leaveSnap (self ): Pass Def snap (self, window = None, rect = None, name = None ): Pass Class MyCaptureGtk (MyCapture ): Def _ init _ (self ): MyCapture. _ init _ (self) Self. window = gtk. Window () Self. window. set_default_size (1, 1) Self. window. connect ("button-press-event", self. button_press_cb) Self. first = None Self. second = None Self. window. show () # Self. window. set_events (gtk. gdk. BUTTON_PRESS_MASK) Def getWindow (self ): Return self. window Def button_press_cb (self, widget, event ): # Print "type is % d" % event. type If event. type = gtk. gdk. BUTTON_PRESS: If event. button = 1: # left button Print "(% d, % d), (% d, % d), button is % d" % (event. x_root, event. y_root, event. x, event. y, event. button) If not self. first: Self. first = MyPoint (event. x_root, event. y_root) Else: Self. second = MyPoint (event. x_root, event. y_root) Self. snap (None, MyRect (self. first, self. second )) Self. first = None Elif event. button = 3: # right button Self. uncapture () Else: Pass Def uncapture (self ): If self. first: Print "cancel" Self. first = None Else: Print "exit now" Gtk. gdk. pointer_ungrab () Gtk. mainquit () Def capture (self, time = 0L ): Cursor = gtk. gdk. Cursor (gtk. gdk. display_get_default (), gtk. gdk. CROSSHAIR) Ret = gtk. gdk. pointer_grab (self. window. window, True, Gtk. gdk. BUTTON_PRESS_MASK, None, cursor, time) If ret = gtk. gdk. GRAB_SUCCESS: Print "left button start, end, right button cancel/exit" Else: Print "failed to capture % d, (viewable % d), (frozen % d), (already % d )"\ % (Ret, gtk. gdk. GRAB_NOT_VIEWABLE, gtk. gdk. GRAB_FROZEN, gtk. gdk. GRAB_ALREADY_GRABBED) Def snap (self, window = None, rect = None, name = None ): W = window If not window: # W = gtk. gdk. get_default_root_window () D = gtk. gdk. display_get_default () W = d. get_default_screen (). get_root_window () R = rect If not r: Sz = w. get_size () R = MyRect (0, 0, sz [0], sz [1]) Print "The size of the window is (% d, % d)" % (r. x, r. y, r. w, r. h) N = name If not n: N = "screenshot.png" Buf = gtk. gdk. Pixbuf (gtk. gdk. COLORSPACE_RGB, False, 8, r. w, r. h) Buf = buf. get_from_drawable (w, w. get_colormap (), r. x, r. y, 0, 0, r. w, r. h) If (buf! = None ): Buf. save (n, "png ") Print "Screenshot saved to % s." % n Else: Print "Unable to get the screenshot ." Def timeout (data ): # Print "timeout" Data. capture () If _ name _ = '_ main __': Cap = MyCaptureGtk () W = cap. getWindow () W. show () Glib. timeout_add_seconds (1, timeout, cap) # Cap. snap () Gtk. main () |