# ClickingsGTK.py (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-08-23 File created. # 2009-02-11 Last modification. # This program demonstrates how we can find out when a mouse # button is pressed. The program prints the position and the # coordinates where mouse button was pressed down. # This program works in the same way as ClickingsApplet.java. # If you want to see a more Java-like version of this program # please read the file ClickingsGTKArray.py. import gtk class ClickingsApplication : def __init__( self ) : self.application_window = gtk.Window( gtk.WINDOW_TOPLEVEL ) self.application_window.set_title( "CLICK THIS WINDOW WITH THE MOUSE" ) self.application_window.connect( "destroy", self.exit_this_application ) self.application_window.set_size_request( 600, 400 ) self.drawing_area = gtk.DrawingArea() self.application_window.add( self.drawing_area ) self.drawing_area.connect( "expose-event", self.drawing_area_exposed ) self.drawing_area.connect( "button_press_event", self.mouse_button_pressed ) self.drawing_area.set_events(gtk.gdk.EXPOSURE_MASK | gtk.gdk.BUTTON_PRESS_MASK ) self.drawing_area.show() self.application_window.show() self.window_width, self.window_height = \ self.application_window.get_size() self.list_of_clicking_coordinates = [] def mouse_button_pressed( self, widget, event ) : self.list_of_clicking_coordinates.append( ( int( event.x ), int( event.y ) ) ) self.drawing_area.queue_draw() return True def drawing_area_exposed( self, area, event ) : graphics_context = \ self.drawing_area.get_style().fg_gc[gtk.STATE_NORMAL] this_drawable = self.drawing_area.window coordinates_as_text = self.drawing_area.create_pango_layout( "" ) for clicking_coordinates in self.list_of_clicking_coordinates : # Below 0 refers to the x coordinate and 1 refers to # the y coordinate. These are stored in a tuple whose length is 2. this_drawable.draw_arc( graphics_context, True, clicking_coordinates[ 0 ] - 3, clicking_coordinates[ 1 ] - 3, 6, 6, 0, 360*64 ) coordinates_as_text.set_text( "(%d, %d)" % clicking_coordinates ) this_drawable.draw_layout( graphics_context, clicking_coordinates[ 0 ] + 7, clicking_coordinates[ 1 ] - 7, coordinates_as_text ) return True def run( self ) : gtk.main() def exit_this_application( self, widget, data=None ) : gtk.main_quit() if __name__ == "__main__": this_application = ClickingsApplication() this_application.run() # The development of this program has been sponsored by # Nokia Multimedia.