Created
September 22, 2019 16:14
-
-
Save Wolf480pl/e41096ca9d417bfc80f285534af9aaff to your computer and use it in GitHub Desktop.
An example python script drawing a basic cat using cairo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python2 | |
import pygtk | |
pygtk.require('2.0') | |
import gtk, gobject, cairo | |
import math | |
def deg2rad(x): | |
return x * math.pi / 180 | |
class Screen(gtk.DrawingArea): | |
# Draw in response to an expose-event | |
__gsignals__ = { "expose-event": "override" } | |
def do_expose_event(self, event): | |
# Create the cairo context | |
cr = self.window.cairo_create() | |
# Restrict Cairo to the exposed area; avoid extra work | |
cr.rectangle(event.area.x, event.area.y, | |
event.area.width, event.area.height) | |
cr.clip() | |
self.draw(cr, *self.window.get_size()) | |
def draw(self, cr, width, height): | |
cr.save() | |
cr.translate(width / 2, height / 2) | |
radius = 40 | |
ear_angle1 = 0 | |
ear_angle2 = 75 | |
ear_offset = radius | |
ear_height = 60 | |
cr.arc(0, 0, radius, deg2rad(-ear_angle1), deg2rad(180 + ear_angle1)) | |
cr.line_to(-ear_offset, -ear_height) | |
cr.arc(0, 0, radius, deg2rad(180 + ear_angle2), deg2rad(-ear_angle2)) | |
cr.line_to(ear_offset, -ear_height) | |
cr.close_path() | |
cr.set_source_rgb(1.0, 1, 0) | |
cr.fill_preserve() | |
cr.set_source_rgb(1.0, 0.75, 0) | |
cr.set_line_width(5) | |
cr.stroke() | |
cr.restore() | |
def run(size=(200, 200)): | |
gtk.threads_init() | |
gtk.threads_enter() | |
window = gtk.Window() | |
widget = Screen() | |
widget.show() | |
window.connect("delete-event", gtk.main_quit) | |
window.add(widget) | |
#window.set_decorated(False) | |
#window.set_skip_taskbar_hint(True) | |
#window.set_skip_pager_hint(True) | |
#window.set_keep_above(True) | |
#window.stick() | |
window.set_default_size(*size) | |
window.present() | |
try: | |
gtk.main() | |
finally: | |
gtk.threads_leave() | |
if __name__ == "__main__": | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment