[pygtk] Close button demonstrating app, and questions

Khiraly khiraly123 at gmx.net
Sun Apr 30 08:31:05 WST 2006


Hi!

(Gian Mario Tagliaretti: thx for the answer to my previous mail)

I have created a mini-app to demonstrating the close button on every
page.
There is one more issue remaining:
When you click on a non-focused tab's close button it close always the
focused notebook page. So the exercise, is obtain the notebook page
number where the clicked close button belongs. (line 55 in the code,
which is actualy not working)

Here is a screenshot of the application:
http://img221.imageshack.us/img221/6805/pygtktutorialnotebook5px.png

Can somebody help me to get working the close button?
The sourcecode: http://pastebin.com/689563

Other things to do:
Get the following keybindings working:
Ctrl-W close the focused tab
Ctrl-PageUP Next tab 
Ctrl-PageDown Previous tab
Ctrl-T New tab

After this, I hope that this simple demo app could be included in the
main tutorial. (the demo notebook app in the tutorial is not really
useful)

Any helps is really appreciated!

Best regards, 
 Khiraly

ps: I Include here the sourcecode to, for the archive.
#!/usr/bin/env python

# example notebook.py

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

class NotebookExample:
    def add_icon_to_button(self,button):
        iconBox = gtk.HBox(False, 0)        
        image = gtk.Image()
        image.set_from_stock(gtk.STOCK_CLOSE,gtk.ICON_SIZE_MENU)
        gtk.Button.set_relief(button,gtk.RELIEF_NONE)
        #gtk.Button.set_focus_on_click(button,False)
        settings = gtk.Widget.get_settings (button);
        (w,h) = gtk.icon_size_lookup_for_settings
(settings,gtk.ICON_SIZE_MENU);
        gtk.Widget.set_size_request (button, w + 4, h + 4);
        image.show()
        iconBox.pack_start(image, True, False, 0)
        button.add(iconBox)
        iconBox.show()
        return 

    def create_custom_tab(self,text, notebook):
        #create a custom tab for notebook containing a 
        #label and a button with STOCK_ICON
        eventBox = gtk.EventBox()
        tabBox = gtk.HBox(False, 2)
        tabLabel = gtk.Label(text)

        tabButton=gtk.Button()
        tabButton.connect('clicked',self.remove_book, notebook)

        #Add a picture on a button
        self.add_icon_to_button(tabButton)
        iconBox = gtk.HBox(False, 0)
        
        eventBox.show()
        tabButton.show()
        tabLabel.show()

        tabBox.pack_start(tabLabel, False)       
        tabBox.pack_start(tabButton, False)

        # needed, otherwise even calling show_all on the notebook won't
        # make the hbox contents appear.
        tabBox.show_all()
        eventBox.add(tabBox)
        return eventBox
    
    # Remove a page from the notebook
    def remove_book(self, button, notebook):
        # VVV TODO!, not working
        page = gtk.Notebook.page_num(button)
        # ^^^ TODO
        notebook.remove_page(page)
        # Need to refresh the widget -- 
        # This forces the widget to redraw itself.
        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)

        # Create a new notebook
        notebook = gtk.Notebook()
        window.add(notebook)
        notebook.show()

        # Add some tab pages for demonstrating
        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("Inside of Frame %d" % page_number)
            frame.add(label)
            label.show()
            
            eventBox = self.create_custom_tab("Tab %d" % page_number,
notebook)
            notebook.append_page(frame, eventBox)
        # Set what page to start at (page 4)
        notebook.set_current_page(3)
        window.show()

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

if __name__ == "__main__":
    NotebookExample()
    main()
    







More information about the pygtk mailing list