| 12
 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
 
 | import pygtk
pygtk.require('2.0')
import gtk
 
class Tableau(gtk.Table):
 
    def __init__(self, x, y):
        gtk.Table.__init__(self, x, y)
        for i in range(x):
            for j in range(y):
                b = gtk.Button(label="(%d,%d)" % (i,j))
                b.connect_object('event', self.ev_button_clicked, b)
                self.attach(b, i, i+1, j, j+1)
 
    def ev_button_clicked(self, button, event):
        if event.type == gtk.gdk.BUTTON_PRESS:
            if event.button == 1:
                item1 = gtk.MenuItem("Fusionner a droite")
                item2 = gtk.MenuItem("Fusionner en bas")
                item1.connect('activate', lambda m: self.fusionner(m, button, "droite"))
                item2.connect('activate', lambda m: self.fusionner(m, button, "bas"))
                menu = gtk.Menu()
                menu.append(item1)
                menu.append(item2)
                menu.show_all()
                menu.popup(None, None, None, event.button, event.time)
                return True
        return False
 
    def fusionner(self, menu, button, direction):
        c = self.coords(button)
        print c, direction
        if direction == "droite":
            c[1] += 1
        if direction == "bas":
            c[3] += 1
        if c[1] > self.get_property('n-columns') or c[3] > self.get_property('n-rows'):
            return
        for w in self.get_children():
            cc = self.coords(w)
            if max(cc[0], c[0]) < min(cc[1], c[1]) and max(cc[2], c[2]) < min(cc[3], c[3]):
                if w is not button:
                    w.destroy()
        self.child_set_property(button, 'left-attach',   c[0])
        self.child_set_property(button, 'right-attach',  c[1])
        self.child_set_property(button, 'top-attach',    c[2])
        self.child_set_property(button, 'bottom-attach', c[3])
        return
 
    def coords(self, button):
        c = self.child_get(button, 'left-attach', 'right-attach', 'top-attach', 'bottom-attach')
        return list(c)
 
#------------------------------------------------------------------------------
 
if __name__ == '__main__':
    win = gtk.Window()
    win.connect('destroy', lambda w: gtk.main_quit())
    t = Tableau(7, 5)
    win.add(t)
    win.show_all()
    gtk.main() | 
Partager