[pygtk] Close button in notebook tab
N. Volbers
mithrandir42 at web.de
Sun May 6 14:01:14 WST 2007
Sylvain Saleur schrieb:
> Hi!
>
> I managed to create a custom tab with a close button. My problem is that
> when I click on a non-focused tab's close button, it close the focused
> tab...
>
> Do you have any suggestion?
>
> I look forward to hearing from you.
>
> Bests.
>
> Sylvain Saleur
>
> Code: (sorry, the comments are in french... The code come from this
> post: http://www.daa.com.au/pipermail/pygtk/2006-April/012216.html )
>
> #!/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éation d'une boite horizontale
> iconBox = gtk.HBox(False, 0) #Création d'une image vide
> image = gtk.Image()
> #On récupère l'icone du bouton "fermer"
> image.set_from_stock(gtk.STOCK_CLOSE,gtk.ICON_SIZE_MENU)
> #On enlève le relief au bouton (donné en attribut)
> gtk.Button.set_relief(button,gtk.RELIEF_NONE)
> #On récupère les propriétés du bouton
> settings = gtk.Widget.get_settings(button)
> #On affecte à w et h les dimensions
> (w,h) =
> 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):
> "Crée une tab customisée avec un label et un bouton fermer"
> #On crée une eventbox
> eventBox = gtk.EventBox()
> #On crée une boite horizontale
> tabBox = gtk.HBox(False, 2)
> #On crée un label "text" (text donné en attribut)
> tabLabel = gtk.Label(text)
> #On crée un bouton
> tabButton=gtk.Button()
> #On lui affecte la méthode remove_book
> tabButton.connect('clicked',self.remove_book, notebook)
>
> #On ajoute l'image au bouton en utilisant la méthode
> add_icon_to_button
> self.add_icon_to_button(tabButton)
> eventBox.show()
> tabButton.show()
> tabLabel.show()
> #On attache label et bouton à la boite
> tabBox.pack_start(tabLabel, False)
> tabBox.pack_start(tabButton, False)
>
> tabBox.show_all()
> #On ajoute la boite à l'eventbox
> eventBox.add(tabBox)
> return eventBox
> def remove_book(self, button, notebook):
> "Fonction de suppression de page"
> #On récupère la page courante
> page = notebook.get_current_page()
> #On la supprime
> notebook.remove_page(page)
> # On actualise le widget
> notebook.queue_draw_area(0,0,-1,-1)
>
> def delete(self, widget, event=None):
> gtk.main_quit()
> return False
>
> def __init__(self):
> window = gtk.Window(gtk.WINDOW_TOPLEVEL)
> window.connect("delete_event", self.delete)
> window.set_border_width(10)
>
> #On crée un nouveau notebook
> notebook = gtk.Notebook()
> window.add(notebook)
> notebook.show()
>
> # On ajoute quelques pages
> for i in range(5):
> page_number = i + 1
> frame = gtk.Frame("Frame %d" % page_number)
> frame.set_border_width(10)
> frame.set_size_request(100, 75)
> frame.show()
> label = gtk.Label("Dans la Frame %d" % page_number)
> frame.add(label)
> label.show()
> eventBox = self.create_custom_tab("Tab %d" %
> page_number, notebook)
> notebook.append_page(frame, eventBox)
> # Page que nous verrons à l'ouverture (page 4)
> notebook.set_current_page(3)
> window.show()
>
> def main():
> gtk.main()
> return 0
>
> if __name__ == "__main__":
> NotebookExample()
> main()
> _______________________________________________
> pygtk mailing list pygtk at daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
>
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 = 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)
More information about the pygtk
mailing list