''' clickablewindow.py Jeff Ondich, updated 3 November 2018 The ClickableWindow class demonstrates how to handle mouse events in the context of John Zelle's graphics library. This sample class uses the Tkinter "bind_all" mechanism to trap left-mouse-button events and respond to them appropriately. The discussion of Events and Bindings in the Tkinter library at http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm describes more things you can do with a mouse. ''' from graphics import * class ClickableWindow(GraphWin): def __init__(self, title, width, height): GraphWin.__init__(self, title, width, height) self.setBackground(color_rgb(255, 255, 255)) self.text_x = width // 2 self.text_y = height // 10 self.text_object = Text(Point(self.text_x, self.text_y), 'Click Somewhere') self.text_object.setSize(24) self.bind_all('', self.mouse_handler) self.text_object.draw(self) def mouse_handler(self, event): dx = event.x - self.text_x dy = event.y - self.text_y self.text_object.setText('Ouch') self.text_object.move(dx, dy) self.text_x += dx self.text_y += dy if __name__ == '__main__': window = ClickableWindow('Mouse Test', 1000, 600) while window.winfo_exists(): window.update()