How to assign icons to `gnotebook()` tabs?

2019-09-06 12:56发布

问题:

I am using gWidgets2 to create a tabbed GUI, and I would like each tab to have an associated icon (image).

Consider:

require(gWidgets2)
w <- gwindow("notebook example", visible=T)
nb <- gnotebook(container=w)
gbutton("Refresh", label="Refresh", container=nb) ## note label argument
gbutton("Info", label="Info", container=nb)

How can I assign the refresh icon next to the label of the 1st tab? And the info icon to the 2nd tab?

回答1:

Something like this can be modified to suit your needs:

function add_stock_icon(nb, nm, page) {
    child <- nb$widget$getNthPage(page-1)
    box <- nb$widget$getTabLabel(child)
    icon <- gimage(stock.id=nm)
    box$packStart(icon$Widget)
}


回答2:

As per the accepted answer, the following does exactly what needed:

add_stock_icon <- function(nb, nm, page, left=TRUE){
    child <- nb$widget$getNthPage(page-1)
    box <- nb$widget$getTabLabel(child)
    icon <- gimage(stock.id=nm)
    box$packStart(icon$widget$parent)
    if(left) box$reorderChild(icon$widget$parent, 0)
}

##add icons to the left of tab labels
add_stock_icon(nb, "refresh", 1)
add_stock_icon(nb, "info", 2)


回答3:

With latest GIT of gWidgets2RGtk2 you can simply do:

nb$add_tab_icon(1, "refresh")
nb$add_tab_icon(2, "info")