简体   繁体   中英

How can i make tabs in pygtk closable?

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()

Here is a small example to show how to create a notebook with tabs and an on_close handler:

import gi
gi.require_version('Gtk', '3.0')

from gi.repository import Gtk as gtk

class Tab():
    def __init__(self, title, parent):
        self.title = title
        self.parent = parent
        self.header = gtk.HBox()
        self.title = gtk.Label(label=title)
        image = gtk.Image()
        image.set_from_stock(gtk.STOCK_CLOSE, gtk.IconSize.MENU)
        close_button = gtk.Button()
        close_button.set_image(image)
        close_button.set_relief(gtk.ReliefStyle.NONE)
        close_button.connect("clicked", self.on_tab_close)

        self.header.pack_start(self.title,
                          expand=True, fill=True, padding=0)
        self.header.pack_end(close_button,
                        expand=False, fill=False, padding=0)
        self.header.show_all()

    def on_tab_close(self, button):
        self.parent.remove_page(self.parent.get_current_page())

class Dash(gtk.Notebook):

    def new_tab(self, title):
        tab = Tab(title, self)
        self.sw = gtk.ScrolledWindow()
        self.page = self.sw
        self.page.add(gtk.Label(label=title))
        self.append_page(self.page, tab.header)


d = Dash()
d.new_tab("first")
d.new_tab("second")
d.new_tab("third")
window = gtk.Window(gtk.WindowType.TOPLEVEL)
window.set_default_size(400, 400);
window.connect("destroy", gtk.main_quit)
window.add(d)
window.show_all()

gtk.main()

输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM