#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2019/11/04 09:29:52 +0900> """ In this program, we connect all mouse clicks with a line. Windows10 x64 1903 + Python 2.7.17 32bit + pygtk 2.24.0 + pycairo 1.8.10 """ # Python 2.7 + PyGTK import pygtk import gtk import cairo pygtk.require("2.0") # Python 3.x + PyGObject # import gi # gi.require_version('Gtk', '3.0') # from gi.repository import Gtk class MouseButton: LEFT_BUTTON = 1 RIGHT_BUTTON = 3 class MyDrawingArea(gtk.DrawingArea): """My DrawingArea.""" def __init__(self): super(MyDrawingArea, self).__init__() self.connect("size-allocate", self.on_self_size_allocate) self.connect("expose-event", self.on_self_expose_event) self.set_events(gtk.gdk.BUTTON_PRESS_MASK) # self.connect("button_press_event", self.on_button_press) self.connect("button-press-event", self.on_button_press) self.coords = [] def on_self_size_allocate(self, widget, allocation): print("resize") self.__width = allocation.width self.__height = allocation.height def on_button_press(self, w, e): if e.type == gtk.gdk.BUTTON_PRESS: if e.button == MouseButton.LEFT_BUTTON: print("LEFT BUTTON") self.coords.append([e.x, e.y]) elif e.button == MouseButton.RIGHT_BUTTON: print("RIGHT BUTTON") self.queue_draw() def on_self_expose_event(self, widget, event=None): """Draw for cairo.""" print("on_draw") c = widget.window.cairo_create() c.set_source_rgb(0, 0, 0) c.set_line_width(0.5) for i in self.coords: for j in self.coords: c.move_to(i[0], i[1]) c.line_to(j[0], j[1]) c.stroke() del self.coords[:] class MyApp(gtk.Window): def __init__(self): super(MyApp, self).__init__() self.connect("destroy", gtk.main_quit) self.darea = MyDrawingArea() self.add(self.darea) self.set_title("Lines") # Python 2.7 + PyGTK self.set_size_request(512, 512) self.set_position(gtk.WIN_POS_CENTER) # Python 3.7 + PyGObject # self.resize(300, 200) # self.set_position(gtk.WindowPosition.CENTER) self.show_all() if __name__ == "__main__": app = MyApp() gtk.main()