#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2019/11/04 09:12:52 +0900> """ This program uses PyCairo to draw on a window in GTK. 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 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) def on_self_size_allocate(self, widget, allocation): """Resize.""" print("resize") self.__width = allocation.width self.__height = allocation.height def on_self_expose_event(self, widget, event): """Draw for pycairo.""" print("draw") ctx = widget.window.cairo_create() ctx.set_source_rgb(0, 0, 0) ctx.select_font_face("Sans", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) ctx.set_font_size(40) ctx.move_to(10, 50) ctx.show_text("Disziplin ist Macht.") class MyApp(object): def __init__(self): self.window = gtk.Window() self.darea = MyDrawingArea() self.window.add(self.darea) self.window.connect("delete-event", self.on_delete_event) self.window.set_title("GTK window") self.window.resize(420, 140) # Python 2.7 + PyGTK self.window.set_position(gtk.WIN_POS_CENTER) # Python 3.7 + PyGObject # self.window.set_position(gtk.WindowPosition.CENTER) def on_delete_event(self, window, event, data=None): gtk.main_quit() return False def main(self): self.window.show_all() gtk.main() if __name__ == "__main__": app = MyApp() app.main()