检查 gtk.notebook 中定义文本的选项卡是否存在 +gtk3

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

有没有函数可以检查 gtk.notebook 中是否有 Tab 定义的文本?刚找到函数 "get_menu_label_text ()",但它只是 returns 从它传输的子标签的文本。

只是想看看是否有已经创建的标签,所以我不必重新创建它。

很简单,但找不到合适的解决方案。

不确定为什么需要这样的功能,因为开发人员应该知道笔记本上发生了什么,因此,它变成了 "trackable"。

总之,有一些方法,比如用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)方法获取页数。

另一种选择是使用 Gtk.Container foreach 方法(Gtk.Notebook 继承自 Gtk.Container)并遍历所有子项并获取选项卡标签文本并将其与搜索文本进行比较。

以下非常简单的示例创建了一个包含未引用文本标签的两页笔记本,然后我们只需验证笔记本标签标签中是否存在某些标签。

示例:

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