Skip to content

Instantly share code, notes, and snippets.

@fawix
Last active October 2, 2015 03:06
Show Gist options
  • Select an option

  • Save fawix/4e55cfb2dc6d302f2093 to your computer and use it in GitHub Desktop.

Select an option

Save fawix/4e55cfb2dc6d302f2093 to your computer and use it in GitHub Desktop.
'''
Created on Oct 1, 2015
@author: fawix
'''
from gi.repository import Gtk, Gdk
import os, sys, cairo, math, random
class FunNewStuff:
start_x_cood = 0 #starting X
start_y_cood = 0 #starting Y
ending_x_cood = 0 #ending X
ending_y_cood = 0 #ending Y
firstClick = False
def __init__(self):
self.window = Gtk.Window()
self.window.set_title ("Drawing Stuff")
self.window.set_border_width(0)
self.window.set_default_size(400, 200)
self.window.connect_after('destroy', self.destroy)
self.window.connect('key-press-event', self.on_key_function)
self.create_vbox()
self.window.add(self.vbox)
self.area = Gtk.DrawingArea()
self.area.connect('draw', self.on_draw)
self.window.connect("motion_notify_event", self.on_motion_notify_event)
self.window.connect("button_press_event", self.on_button_press_event)
self.area.set_events(Gdk.EventType.MOTION_NOTIFY)
#http://lazka.github.io/pgi-docs/Gdk-3.0/classes/Window.html#Gdk.Window.set_event_compression
self.vbox.add (self.area)
self.area.props.expand = True
self.window.show_all ()
self.window.fullscreen()
self.window.set_opacity(0.5)
def on_motion_notify_event (self, widget, event):
print("on_motion_notify_event")
if event.is_hint:
x, y, state = event.window.get_pointer()
else:
x = event.x
y = event.y
state = event.state
if self.firstClick :
self.ending_x_cood = x
self.ending_y_cood = y
self.draw_rectangle(self, widget, self.start_x_cood, self.start_y_cood, self.ending_x_cood, self.ending_y_cood)
return True
def on_button_press_event (self, widget, event):
print("on_button_press_event x:{0} y:{1} - button: {2}".format(event.x, event.y, event.button))
print("on_button_press_event {0}".format(event.type))
if not self.firstClick and event.button == 1 :
self.firstClick = True
self.start_x_cood = event.x
self.start_y_cood = event.y
self.draw_rectangle(self, widget, event.x, event.y , event.x, event.y)
def on_key_function (self, widget, event):
if event.keyval == Gdk.KEY_Escape :
self.on_cancel_action()
def create_vbox (self):
self.vbox = Gtk.Box()
self.vbox.set_spacing (0)
self.vbox.set_orientation (Gtk.Orientation.VERTICAL)
def on_cancel_action (self):
self.window.unfullscreen()
self.window.set_opacity(1)
self.window.show_all()
#self | widget = drawing area | cr = cairo Context
def on_draw(self, widget, cr):
#print ("drawing")
cr.set_source_rgba(0.5, 0.5, 0.5, 0.5)
cr.rectangle(0, 0, widget. get_allocated_width(), widget. get_allocated_height())
cr.fill()
cr.set_source_rgba(1, 1, 1, 1)
cr.rectangle(50, 50, 100, 100)
cr.fill()
def draw_rectangle (self, widget, start_x_cood, start_y_cood, ending_x_cood, ending_y_cood, **kwargs):
print ("draw_retangle")
cr = cairo.Context()
cr.set_source_rgba(1, 1, 1, 1)
cr.rectangle(start_x_cood, start_y_cood, ending_x_cood, ending_y_cood)
cr.fill()
def reset (self):
self.start_x_cood = 0
self.start_y_cood = 0
self.ending_x_cood = 0
self.ending_y_cood = 0
self.firstClick = False
def destroy(self, window):
print("terminated")
Gtk.main_quit()
def main () :
win = FunNewStuff()
Gtk.main()
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment