1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#!/usr/bin/python
import gtk
from random import randrange
# Dessiner une ligne sur la zone de dessin
def expose(widget, data):
global x1, x2, y1, y2, couleur
cr = data.window.cairo_create()
cr.set_source_rgb(couleur[0], couleur[1], couleur[2])
cr.set_line_width(2)
#w = data.allocation.width
#h = data.allocation.height
#print w, h
cr.move_to(x1, y1)
cr.line_to(x2, y2)
cr.stroke()
y2, y1 = y2+10, y1-10
# Choisir une couleur au hasard
def color(widget):
'test'
global couleur
i = 0
while i<3:
couleur[i] = randrange(0,255)/255.0
i += 1
############ Gestion Pixmap
# Create a new backing pixmap of the appropriate size
def configure_event(widget, event):
global pixmap
x, y, width, height = widget.get_allocation()
pixmap = gtk.gdk.Pixmap(widget.window, width, height)
pixmap.draw_rectangle(widget.get_style().white_gc,
True, 0, 0, width, height)
return True
# Redraw the screen from the backing pixmap
def expose_event(widget, event):
x , y, width, height = event.area
widget.window.draw_drawable(widget.get_style().fg_gc[gtk.STATE_NORMAL],
pixmap, x, y, x, y, width, height)
return False
def button_press_event(widget, event):
if event.button == 1 and pixmap != None:
expose(widget, event.x, event.y)
return True
def motion_notify_event(widget, event):
if event.is_hint:
x, y, state = event.window.get_pointer()
else:
x = event.x
y = event.y
state = event.state
if state & gtk.gdk.BUTTON1_MASK and pixmap != None:
expose(widget, x, y)
return True
#################
# Initialisation des variables
couleur = [0, 0, 0]
x1, y1, x2, y2 = 30, 30, 570, 570
# Backing pixmap for drawing area
pixmap = None
# Cr?ation de l'interface
PyApp = gtk.Window()
PyApp.set_title("Formes")
PyApp.set_size_request(800, 600)
PyApp.set_border_width(20)
PyApp.set_position(gtk.WIN_POS_CENTER)
PyApp.connect("destroy", gtk.main_quit)
darea = gtk.DrawingArea()
###################### Copier-coller pour gestion de l'affichage
# Signals used to handle backing pixmap
darea.connect("expose_event", expose_event)
darea.connect("configure_event", configure_event)
# Event signals
darea.connect("motion_notify_event", motion_notify_event)
darea.connect("button_press_event", button_press_event)
darea.set_events(gtk.gdk.EXPOSURE_MASK
| gtk.gdk.LEAVE_NOTIFY_MASK
| gtk.gdk.BUTTON_PRESS_MASK
| gtk.gdk.POINTER_MOTION_MASK
| gtk.gdk.POINTER_MOTION_HINT_MASK)
##################
# Suite de l'interface
button1 = gtk.Button(label="Tracer une ligne")
button1.set_size_request(130, 35)
button1.connect("clicked", expose, darea)
button2 = gtk.Button(label="Changer de couleur")
button2.set_size_request(130, 35)
button2.connect("clicked", color)
button3 = gtk.Button(label="Quitter")
button3.set_size_request(130, 35)
button3.connect("clicked", gtk.main_quit)
vbox = gtk.VBox(False, 5)
hbox = gtk.HBox(False, 3)
vbox.pack_start(button1, False, True, 3)
vbox.pack_start(button2, False, False, 3)
vbox.pack_end(button3, False, False, 3)
hbox.pack_start(darea, True, True)
hbox.pack_end(vbox, False, False)
PyApp.add(hbox)
PyApp.show_all()
gtk.main() |
Partager