check if tab with defined text in gtk.notebook exists +gtk3

python-3.x gtk3 glade

85 观看

1回复

164 作者的声誉

Is there a function to check if there is a Tab in a gtk.notebook with a defined text? Just found the function "get_menu_label_text ()", but it just returns the text of the tab from it's transmitted child.

Just want to find out if there is a already created tab, so I don't have to create it again.

Really simple, but can't find a proper solution.

作者: Benny H. 的来源 发布者: 2017 年 12 月 27 日

回应 1


1

3367 作者的声誉

决定

Not sure why you need such function as the developer should know what goes on the notebook and as such, it becomes "trackable".

Anyway, there are some approaches, like getting the number of pages with get_n_pages(), getting the child for the n page with get_nth_page() in a for loop and invoking the Gtk.Notebook get_tab_label_text(child) method.

Another option would be using the Gtk.Container foreach method (Gtk.Notebook inherits from Gtk.Container) and iterate by all child(s) and get the tab label text and compare it with the search text.

The following, very simple, example creates a two page notebook with unreferenced text labels and then we simply verify if some label exists in the Notebook tab labels.

Example:

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

class MyWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Simple Notebook Example")
        self.set_border_width(3)

        self.notebook = Gtk.Notebook()
        self.add(self.notebook)

        self.page1 = Gtk.Box()
        self.page1.set_border_width(10)
        self.page1.add(Gtk.Label('This is Gtk.Notebook Page X'))
        self.notebook.append_page(self.page1, Gtk.Label('Page X'))

        self.page2 = Gtk.Box()
        self.page2.set_border_width(10)
        self.page2.add(Gtk.Label('This is Gtk.Notebook Page Y'))
        self.notebook.append_page(self.page2, Gtk.Label('Page Y'))

    def check_exists_tab_with_label(self, label):
        self.notebook.foreach(self.check_label_for_child, label) 

    def check_label_for_child(self, widget, label):
        if (self.notebook.get_tab_label_text(widget) == label):
            print ("FOUND")

win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
win.check_exists_tab_with_label('Page Y')
Gtk.main()
作者: José Fonte 发布者: 2017 年 12 月 27 日
32x32