Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import gi
- gi.require_version('Gtk', '3.0')
- from gi.repository import Gtk, Gdk
- from random import random
- COLORS = [
- [(1,0,0), (1,0,0), (1,0,0)],
- [(1,0,0), (1,0,0), (1,0,0)],
- [(1,0,0), (1,0,0), (1,0,0)]
- ]
- TYPES = [
- [1, 0, 1],
- [0, 1, 0],
- [1, 0, 1]
- ]
- def get_size_and_scale(da):
- ws = da.get_allocation()
- w = len(TYPES[0])
- h = len(TYPES)
- xs = ws.width / w
- ys = ws.height / h
- return w, h, xs, ys
- def draw(da, ctx):
- ctx.set_source_rgb(0, 0, 0)
- ctx.paint()
- w, h, xs, ys = get_size_and_scale(da)
- gap = 10
- for y in range(h):
- for x in range(w):
- if TYPES[y][x] == 1:
- ctx.rectangle(x*xs+gap/2, y*ys+gap/2, xs-gap, ys-gap)
- else:
- ctx.arc(x*xs+xs/2, y*ys+ys/2, (min(xs,ys)-gap)/2, 0, 6.28)
- ctx.set_source_rgb(*COLORS[y][x])
- ctx.fill()
- def click(da, e):
- global COLORS
- w, h, xs, ys = get_size_and_scale(da)
- x = int(e.x / xs)
- y = int(e.y / ys)
- COLORS[y][x] = (random(), random(), random())
- da.queue_draw()
- def main():
- drawingarea = Gtk.DrawingArea()
- drawingarea.set_events(Gdk.EventMask.BUTTON_PRESS_MASK)
- drawingarea.connect('draw', draw)
- drawingarea.connect('button-press-event', click)
- win = Gtk.Window()
- win.set_default_size(500, 500)
- win.connect('destroy', lambda w: Gtk.main_quit())
- win.add(drawingarea)
- win.show_all()
- Gtk.main()
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment