Guest User

Untitled

a guest
Feb 25th, 2017
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. import gi
  2. gi.require_version('Gtk', '3.0')
  3.  
  4. from gi.repository import Gtk, Gdk
  5. from random import random
  6.  
  7.  
  8. COLORS = [
  9.             [(1,0,0), (1,0,0), (1,0,0)],
  10.             [(1,0,0), (1,0,0), (1,0,0)],
  11.             [(1,0,0), (1,0,0), (1,0,0)]
  12.         ]
  13.        
  14. TYPES = [
  15.             [1, 0, 1],
  16.             [0, 1, 0],
  17.             [1, 0, 1]
  18.         ]
  19.  
  20.  
  21. def get_size_and_scale(da):
  22.     ws = da.get_allocation()
  23.     w = len(TYPES[0])
  24.     h = len(TYPES)
  25.     xs = ws.width / w
  26.     ys = ws.height / h
  27.     return w, h, xs, ys
  28.  
  29.  
  30. def draw(da, ctx):
  31.    
  32.     ctx.set_source_rgb(0, 0, 0)
  33.     ctx.paint()
  34.    
  35.     w, h, xs, ys = get_size_and_scale(da)
  36.     gap = 10
  37.    
  38.     for y in range(h):
  39.         for x in range(w):
  40.             if TYPES[y][x] == 1:
  41.                 ctx.rectangle(x*xs+gap/2, y*ys+gap/2, xs-gap, ys-gap)
  42.             else:
  43.                 ctx.arc(x*xs+xs/2, y*ys+ys/2, (min(xs,ys)-gap)/2, 0, 6.28)
  44.             ctx.set_source_rgb(*COLORS[y][x])
  45.             ctx.fill()
  46.            
  47.  
  48. def click(da, e):
  49.     global COLORS
  50.    
  51.     w, h, xs, ys = get_size_and_scale(da)
  52.    
  53.     x = int(e.x / xs)
  54.     y = int(e.y / ys)
  55.     COLORS[y][x] = (random(), random(), random())
  56.  
  57.     da.queue_draw()
  58.    
  59.    
  60. def main():
  61.     drawingarea = Gtk.DrawingArea()
  62.     drawingarea.set_events(Gdk.EventMask.BUTTON_PRESS_MASK)
  63.     drawingarea.connect('draw', draw)
  64.     drawingarea.connect('button-press-event', click)
  65.  
  66.     win = Gtk.Window()
  67.     win.set_default_size(500, 500)
  68.     win.connect('destroy', lambda w: Gtk.main_quit())
  69.     win.add(drawingarea)
  70.     win.show_all()
  71.    
  72.     Gtk.main()
  73.  
  74.  
  75. if __name__ == '__main__':
  76.     main()
Add Comment
Please, Sign In to add comment