'''The ClickableWindow class demonstrates how to handle mouse events. Jeff Ondich, 25 Feb 2007 This sample class uses the Tkinter "bind_all" mechanism to trap left-mouse-button events and respond to them appropriately. The ClickableWindow class inherits from John Zelle's GraphWin class, so to use this sample, you need to have Zelle's "graphics" module. The discussion of Events and Bindings in the Tkinter library at http://www.pythonware.com/library/tkinter/introduction/events-and-bindings.htm describes more things you can do with a mouse. ''' from graphics import * class ClickableWindow(GraphWin): def __init__(self, *args): GraphWin.__init__(self, *args) self.setBackground(color_rgb(255, 255, 255)) self.textX = 500 self.textY = 50 self.textObject = Text(Point(self.textX, self.textY), 'Click Somewhere') self.bind_all('', self.mouseHandler) self.textObject.draw(self) def mouseHandler(self, event): dx = event.x - self.textX dy = event.y - self.textY self.textObject.setText('Ouch') self.textObject.move(dx, dy) self.textX += dx self.textY += dy if __name__ == '__main__': window = ClickableWindow('Mouse Test', 1000, 600) while window.winfo_exists(): window.update()