[pygtk] Close button in notebook tab [solved]

Sylvain Saleur syeric1 at gmail.com
Sun May 6 22:45:35 WST 2007


>
> N. Volbers wrote:
>
> You can use notebook.remove(widget) to remove the proper notebook tab.
> Of course, then the callback needs to know, which widget it should
> remove. I suggest passing the frame to the create_custom_tab method:
>
>     eventBox =3D self.create_custom_tab("Tab %d" % page_number, notebook,
> frame)
>
> Then, in create_custom_tab, you add the frame to the connect method:
>
>     tabButton.connect('clicked',self.remove_book, notebook, frame)
>
> And finally, in the remove_book method, you know exactly which tab to
> remove:
>
>     notebook.remove_page(frame)
>
>
> Hi!

It works well!

Thank you very much!

Bests

Sylvain Saleur

Here the updated code for the next who want to know:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

#  notebook.py

import pygtk
pygtk.require('2.0')
import gtk

class NotebookExample:
    def add_icon_to_button(self,button):
    "Fonction pour ajouter un bouton fermer"
    #cr=E9ation d'une boite horizontale
        iconBox =3D gtk.HBox(False, 0)
    #Cr=E9ation d'une image vide
        image =3D gtk.Image()
    #On r=E9cup=E8re l'icone du bouton "fermer"
        image.set_from_stock(gtk.STOCK_CLOSE,gtk.ICON_SIZE_MENU)
    #On enl=E8ve le relief au bouton (donn=E9 en attribut)
        gtk.Button.set_relief(button,gtk.RELIEF_NONE)
    #On r=E9cup=E8re les propri=E9t=E9s du bouton
        settings =3D gtk.Widget.get_settings(button)
    #On affecte =E0 w et h les dimensions
        (w,h) =3D gtk.icon_size_lookup_for_settings(settings,
gtk.ICON_SIZE_MENU)
    #On modifie ces dimensions
        gtk.Widget.set_size_request(button, w + 4, h + 4)
        image.show()
    #On met l'image dans la boite
        iconBox.pack_start(image, True, False, 0)
    #On ajoute la boite dans le bouton
        button.add(iconBox)
        iconBox.show()
        return

    def create_custom_tab(self,text, notebook, frame):
        "Cr=E9e une tab customis=E9e avec un label et un bouton fermer"
    #On cr=E9e une eventbox
        eventBox =3D gtk.EventBox()
    #On cr=E9e une boite horizontale
        tabBox =3D gtk.HBox(False, 2)
    #On cr=E9e un label "text" (text donn=E9 en attribut)
        tabLabel =3D gtk.Label(text)
    #On cr=E9e un bouton
        tabButton=3Dgtk.Button()
    #On lui affecte la m=E9thode remove_book
        tabButton.connect('clicked',self.remove_book, notebook, frame)

        #On ajoute l'image au bouton en utilisant la m=E9thode
add_icon_to_button
        self.add_icon_to_button(tabButton)

        eventBox.show()
        tabButton.show()
        tabLabel.show()
    #On attache label et bouton =E0 la boite
        tabBox.pack_start(tabLabel, False)
        tabBox.pack_start(tabButton, False)

        tabBox.show_all()
    #On ajoute la boite =E0 l'eventbox
        eventBox.add(tabBox)
        return eventBox


    def remove_book(self, button, notebook, frame):
    "Fonction de suppression de page"
    #On r=E9cup=E8re la page courante
        page =3D notebook.get_current_page()
    #On la supprime
        notebook.remove(frame)
        # On actualise le widget
        notebook.queue_draw_area(0,0,-1,-1)

    def delete(self, widget, event=3DNone):
        gtk.main_quit()
        return False

    def __init__(self):
        window =3D gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.connect("delete_event", self.delete)
        window.set_border_width(10)

        #On cr=E9e un nouveau notebook
        notebook =3D gtk.Notebook()
        window.add(notebook)
        notebook.show()

        # On ajoute quelques pages
        for i in range(5):
            page_number =3D i + 1
            frame =3D gtk.Frame("Frame %d" % page_number)
            frame.set_border_width(10)
            frame.set_size_request(100, 75)
            frame.show()
            label =3D gtk.Label("Dans la Frame %d" % page_number)
            frame.add(label)
            label.show()

            eventBox =3D self.create_custom_tab("Tab %d" % page_number,
notebook, frame)
            notebook.append_page(frame, eventBox)
        # Page que nous verrons =E0 l'ouverture (page 4)
        notebook.set_current_page(3)
        window.show()

def main():
    gtk.main()
    return 0

if __name__ =3D=3D "__main__":
    NotebookExample()
    main()
-------------- section suivante --------------
Une pi?ce jointe HTML a ?t? enlev?e...
URL: http://www.daa.com.au/pipermail/pygtk/attachments/20070506/0f88b467/at=
tachment-0001.htm


More information about the pygtk mailing list