i am using python with gtk, trying to make a simple text editor with tabs, i am still new, so i wonder how can i make the tabs closable and reordable ? it is much easier in qt, but i loved gtk more. this is my code:
import gtk
from tab import *
class Dash(gtk.Notebook):
def __init__(self):
super(gtk.Notebook,self).__init__()
self.defaultTab()
def defaultTab(self):
tab = Tab()
self.append_page(tab.child,tab.label)
the other module "tab" has some variables :
from launchers import *
class Tab():
def __init__(self):
self.label = gtk.Label("New Tab")
self.type = "dash"
launchers = Launchers()
self.child = launchers
so what i gotta do ?
Instead of using a gtk.Label
when appending a new page to the gtk.Notebook
, you need to create a gtk.HBox
that contains both a gtk.Label
and a gtk.Button
. More or less, something like this:
class Dash(gtk.Notebook):
...
def defaultTab(self):
self.append_page(tab.child,tab.header)
...
class Tab():
def __init__(self):
...
header = gtk.HBox()
title_label = gtk.Label()
image = gtk.Image()
image.set_from_stock(gtk.STOCK_CLOSE, gtk.ICON_SIZE_MENU)
close_button = gtk.Button()
close_button.set_image(image)
close_button.set_relief(gtk.RELIEF_NONE)
self.connect(close_button, 'clicked', self.close_cb)
header.pack_start(title_label,
expand=True, fill=True, padding=0)
header.pack_end(close_button,
expand=False, fill=False, padding=0)
header.show_all()
self.header = header
...
This is just to display the close button. To actually close the tab, you'll need to handle the clicked
signal from the button.
To make the tab reorderable, do this after your append_page()
call:
self.child_set_property(tab.child, 'reorderable', True)
or:
self.set_tab_reorderable(tab.child, True)
Here's a short example:
import gtk
win = gtk.Window(gtk.WINDOW_TOPLEVEL)
notebook = gtk.Notebook()
page1 = gtk.Label('This is the first page')
page2 = gtk.Label('This is the second page')
notebook.append_page(page1, gtk.Label('P1'))
notebook.append_page(page2, gtk.Label('P2'))
notebook.props.border_width = 12
notebook.set_tab_reorderable(page1, True)
notebook.set_tab_reorderable(page2, True)
win.add(notebook)
win.connect('delete-event', gtk.main_quit)
win.show_all()
gtk.main()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With