Hello.

I just copy a small program I've done which generate a notebook with a
close button. Hopefully it will be helpful for you.

#!/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):
    "Function that add the icon to the button"
    #Hbox creation
        iconBox = gtk.HBox(False, 0)
    #Empty image creation
        image = gtk.Image()
    #Let's get the default gtk close button
        image.set_from_stock(gtk.STOCK_CLOSE,gtk.ICON_SIZE_MENU)
    #set the relief off
        gtk.Button.set_relief(button,gtk.RELIEF_NONE)
    #Get the settings of the button
        settings = gtk.Widget.get_settings(button)
    #w and h dimensions of the button
        (w,h) = gtk.icon_size_lookup_for_settings(settings,gtk.ICON_SIZE_MENU)
    #modification of the dimensions
        gtk.Widget.set_size_request(button, w + 4, h + 4)
        image.show()
    #pack the image in the box
        iconBox.pack_start(image, True, False, 0)
    #add the box in the button
        button.add(iconBox)
        iconBox.show()
        return

    def create_custom_tab(self,text, notebook, frame):
        "Create a custom tab with a label and the button"
    #eventbox creation
        eventBox = gtk.EventBox()
    #Hbox creation
        tabBox = gtk.HBox(False, 2)
    #"text" label creation
        tabLabel = gtk.Label(text)
    #creation of a button
        tabButton=gtk.Button()
    #connect to the "remove_book" function
        tabButton.connect('clicked',self.remove_book, notebook, frame)

        #add the icon with the previously defined add_icon_to_button
        self.add_icon_to_button(tabButton)

        eventBox.show()
        tabButton.show()
        tabLabel.show()
    #add label and button
        tabBox.pack_start(tabLabel, False)
        tabBox.pack_start(tabButton, False)

        tabBox.show_all()
    #add the box to the eventox
        eventBox.add(tabBox)
        return eventBox


    def remove_book(self, button, notebook, frame):
    "Function to remove the page"
    #suppress the page. You must give the child widget of the page
        notebook.remove(frame)
        # you call the previous page
        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)

        #book generation
        notebook = gtk.Notebook()
        window.add(notebook)
        notebook.show()

        # some 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("In the Frame %d" % page_number)
            frame.add(label)
            label.show()

            eventBox = self.create_custom_tab("Tab %d" % page_number,
notebook, frame)
            notebook.append_page(frame, eventBox)
        # page you see at the opening
        notebook.set_current_page(3)
        window.show()

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

if __name__ == "__main__":

    NotebookExample()

    main()

I'm sure we can do that more quickly but it works and as you can see
in the code style, I'm not a specialist...

Hope it will help you.

Sylvain

2008/1/7, [EMAIL PROTECTED] <[EMAIL PROTECTED]>:
>
> In gtk notebook page_num(child) being a widget
> Returns : the index of the page containing child, or -1
>
> So here is the problem:
>
> Added button to tab for close so I need the page the button is on since the
> button grabs focus rather than the tab. I tried emit back to parent and that
> failed to produce anything.
>
> The code segements:
>
> Creation:
>    #--------------------------------------------------------------------------
>    def new_page(self, arg, filename):
>       self.z = self.z + 1
>       # create  scroller, gtksourceview, buffer and set mime
>       scroller = gtk.ScrolledWindow()
>       scroller.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
>       bufferS = gtksourceview.SourceBuffer()
>       manager = gtksourceview.SourceLanguagesManager()
>       if arg:
>          type = gnomevfs.get_mime_type(arg)
>          language = manager.get_language_from_mime_type(type)
>       else:
>          language = manager.get_language_from_mime_type('text')
>       bufferS.set_language(language)
>       view = gtksourceview.SourceView(bufferS)
>       scroller.add(view)
>       # set defaults for gtksourceview and buffer
>       bufferS.set_highlight(self.HIGHLIGHT)
>       bufferS.set_check_brackets(self.CHECK_BRACKETS)
>       view.set_show_line_numbers(self.SHOW_LINE_NUMBERS)
>       view.set_show_margin(self.SHOW_MARGIN)
>       view.set_auto_indent(self.AUTO_INDENT)
>       view.set_tabs_width(int(self.TABS_WIDTH))
>       
> view.set_insert_spaces_instead_of_tabs(self.INSERT_SPACES_INSTEAD_OF_TABS)
>       view.set_margin(int(self.MARGIN))
>       # add close button
>       image = gtk.Image()
>       image.set_from_file("data/images/close.png")
>       btn = gtk.Button()
>       btn.add(image)
>       btn.connect('clicked',self.on_close_btn)
>       #btn.set_size_request(16,16)
>       # add label
>       hbox = gtk.HBox(False,3)
>       if not arg:
>          self.key[self.z] = gtk.Label(filename)
>       else:
>          self.key[self.z] = gtk.Label(filename.rsplit('/',1)[1])
>       hbox.add(self.key[self.z])
>       hbox.add(btn)
>       hbox.show_all()
>       # load and set text
>       if arg:
>          try:
>             bufferS.begin_not_undoable_action()
>             f = open(arg, 'r')
>             bufferS.set_text(f.read())
>             f.close()
>             bufferS.end_not_undoable_action()
>          except:
>             print 'File Error: '+arg
>       else:
>          bufferS.set_text('')
>       # populate tree
>       iter = None
>       iter = self.mod.append(iter)
>       if arg:
>          self.mod.set(iter,0, arg)
>       else:
>          self.mod.set(iter,0, filename)
>       self.mod.set(iter,1, self.key[self.z].get_text())
>       self.mod.set(iter,2, view)
>       self.mod.set(iter,3, bufferS)
>       self.mod.set(iter,4, self.key[self.z])
>       self.view = view
>       self.bufferS = bufferS
>       # add page
>       page = self.mdi_book.append_page(scroller,hbox)
>       self.mdi_book.show_all()
>       # switch to page
>       self.mdi_book.set_current_page(page)
>
> Callbacks:
>     
> #--------------------------------------------------------------------------
>    def on_close_btn(self, widget):
>       page = self.mdi_book.page_num(widget)
>       print page
>    #--------------------------------------------------------------------------
>    def on_close(self, widget):
>       p = self.mdi_book.get_current_page()
>       self.on_update_tree('delete')
>       self.mdi_book.remove_page(p)
>
>
>
>
>
> _______________________________________________
> Join Excite! - http://www.excite.com
> The most personalized portal on the Web!
>
>
> _______________________________________________
> pygtk mailing list   pygtk@daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
>
_______________________________________________
pygtk mailing list   pygtk@daa.com.au
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/

Reply via email to