Entries in the Category “gtk”

Two small pygtk recipes : autohide and notebook window creation

written by nicoe, on Feb 11, 2011 2:23:00 PM.

Here are two small python + GTk recipes that might comes handy when people are searching for howtos about autohide and window creation from notebook drag and drop.

Auto-hiding part of a window with pygtk

First you create a gtk.Paned widget wich will hold the main screen and the widgets that will be hidden automatically. Then in the left part of pane window the trick is to use an EventBox around your VBox so that you can listen for the enter-notify-event and leave-notify-event signals.

import gtk

def toggle_max(widget, event):
    pane.set_position(65)

def toggle_min(widget, event):
    pane.set_position(15)

window = gtk.Window()
pane = gtk.HPaned()
eventbox = gtk.EventBox()
vbox = gtk.VBox()
eventbox.add(vbox)
entry1 = gtk.Entry()
vbox.pack_start(entry1)
entry2 = gtk.Entry()
pane.add1(eventbox)
pane.add2(entry2)

eventbox.connect('enter-notify-event', toggle_max)
eventbox.connect('leave-notify-event', toggle_min)

window.add(pane)
window.show_all()

gtk.main()

Notebook window creation

One very neat feature I discovered with chromium is the creation of another window once you drag and drop a tab in your root window. Since I am thinking about integrating this feature in the tryton client, I give it a shot in a very small script. The tricky part was debugging the create_window_from_tab function, kudos go to Juhaz from #pygtk who helped me with this.

import gtk

def check_pages(notebook, child, page_num):
    page_nbr = notebook.get_n_pages()
    if page_nbr == 0:
        parent_win = notebook.get_parent_window()
        parent_win.destroy()

def create_window_from_tab(notebook, page, drop_x, drop_y):
    window = gtk.Window()
    nb = gtk.Notebook()
    nb.set_group_id(42)
    nb.connect('create-window', create_window_from_tab)
    nb.connect('page-removed', check_pages)
    window.add(nb)
    window.show_all()
    window.maximize()
    return nb

dialog = gtk.Window()
image_tab1 = gtk.Image()
image_tab1.set_from_stock(gtk.STOCK_DIALOG_AUTHENTICATION, gtk.ICON_SIZE_BUTTON)
label_tab1 = gtk.Label('Tab 1')
image_tab2 = gtk.Image()
image_tab2.set_from_stock(gtk.STOCK_DIALOG_INFO, gtk.ICON_SIZE_BUTTON)
label_tab2 = gtk.Label('Tab 2')
image_tab3 = gtk.Image()
image_tab3.set_from_stock(gtk.STOCK_DIALOG_ERROR, gtk.ICON_SIZE_BUTTON)
label_tab3 = gtk.Label('Tab 3')

notebook = gtk.Notebook()
notebook.set_group_id(42)
notebook.append_page(image_tab1, label_tab1)
notebook.set_tab_detachable(image_tab1, True)
notebook.append_page(image_tab2, label_tab2)
notebook.set_tab_detachable(image_tab2, True)
notebook.append_page(image_tab3, label_tab3)
notebook.set_tab_detachable(image_tab3, True)
notebook.connect('create-window', create_window_from_tab)
notebook.connect('page-removed', check_pages)

dialog.add(notebook)
dialog.show_all()

gtk.main()