Vous êtes à peu près ici : Accueil  »   tutoriel PyGTK  »   PyGTK : sommaire

Annexe B. Exemples de code

Table des matières

B.1. scribblesimple.py

B.1. scribblesimple.py

     1   #!/usr/bin/env python
     2   # -*- coding:utf-8 -*-
     3   # exemple scribblesimple.py
     4   
     5    # GTK - The GIMP Toolkit
     6    # Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
     7    # Copyright (C) 2001-2004 John Finlay
     8    #
     9    # This library is free software; you can redistribute it and/or
    10    # modify it under the terms of the GNU Library General Public
    11    # License as published by the Free Software Foundation; either
    12    # version 2 of the License, or (at your option) any later version.
    13    #
    14    # This library is distributed in the hope that it will be useful,
    15    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    16    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    17    # Library General Public License for more details.
    18    #
    19    # You should have received a copy of the GNU Library General Public
    20    # License along with this library; if not, write to the
    21    # Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    22    # Boston, MA 02111-1307, USA.
    23   
    24   
    25   import pygtk
    26   pygtk.require('2.0')
    27   import gtk
    28   
    29   # Pixmap d'arrière-plan pour la zone de dessin
    30   pixmap = None
    31   
    32   # Création d'un nouveau pixmap d'arrière-plan de la taille voulue
    33   def configure_event(widget, event):
    34       global pixmap
    35   
    36       x, y, largeur, hauteur = widget.get_allocation()
    37       pixmap = gtk.gdk.Pixmap(widget.window, largeur, hauteur)
    38       pixmap.draw_rectangle(widget.get_style().white_gc,
    39                             True, 0, 0, largeur, hauteur)
    40   
    41       return True
    42   
    43   # Redessine l'écran à partir du pixmap d'arrière-plan
    44   def expose_event(widget, event):
    45       x , y, largeur, hauteur = event.area
    46       widget.window.draw_drawable(widget.get_style().fg_gc[gtk.STATE_NORMAL],
    47                                   pixmap, x, y, x, y, largeur, hauteur)
    48       return False
    49   
    50   # Dessine un rectangle sur l'écran
    51   def brosse_dessin(widget, x, y):
    52       rect = (int(x-5), int(y-5), 10, 10)
    53       pixmap.draw_rectangle(widget.get_style().black_gc, True,
    54                             rect[0], rect[1], rect[2], rect[3])
    55       widget.queue_draw_area(rect[0], rect[1], rect[2], rect[3])
    56   
    57   def bouton_press_event(widget, event):
    58       if event.button == 1 and pixmap != None:
    59           brosse_dessin(widget, event.x, event.y)
    60       return True
    61   
    62   def motion_notify_event(widget, event):
    63       if event.is_hint:
    64           x, y, etat = event.window.get_pointer()
    65       else:
    66           x = event.x
    67           y = event.y
    68           etat = event.state
    69       
    70       if etat & gtk.gdk.BUTTON1_MASK and pixmap != None:
    71           brosse_dessin(widget, x, y)
    72     
    73       return True
    74   
    75   def main():
    76       fenetre = gtk.Window(gtk.WINDOW_TOPLEVEL)
    77       fenetre.set_name ("Test Input")
    78   
    79       boitev = gtk.VBox(False, 0)
    80       fenetre.add(boitev)
    81       boitev.show()
    82   
    83       fenetre.connect("destroy", lambda w: gtk.main_quit())
    84   
    85       # Création de la zone de dessin
    86       zone_dessin = gtk.DrawingArea()
    87       zone_dessin.set_size_request(200, 200)
    88       boitev.pack_start(zone_dessin, True, True, 0)
    89   
    90       zone_dessin.show()
    91   
    92       # Signaux utilisés pour gérer le pixmap hors écran
    93       zone_dessin.connect("expose_event", expose_event)
    94       zone_dessin.connect("configure_event", configure_event)
    95   
    96       # Signaux d'événements
    97       zone_dessin.connect("motion_notify_event", motion_notify_event)
    98       zone_dessin.connect("button_press_event", bouton_press_event)
    99   
   100       zone_dessin.set_events(gtk.gdk.EXPOSURE_MASK
   101                               | gtk.gdk.LEAVE_NOTIFY_MASK
   102                               | gtk.gdk.BUTTON_PRESS_MASK
   103                               | gtk.gdk.POINTER_MOTION_MASK
   104                               | gtk.gdk.POINTER_MOTION_HINT_MASK)
   105   
   106       # .. Et un bouton quitter
   107       bouton = gtk.Button("Quit")
   108       boitev.pack_start(bouton, False, False, 0)
   109   
   110       bouton.connect_object("clicked", lambda w: w.destroy(), fenetre)
   111       bouton.show()
   112   
   113       fenetre.show()
   114   
   115       gtk.main()
   116   
   117       return 0
   118   
   119   if __name__ == "__main__":
   120       main()