为什么我的 Gtk.Notebook 不能正确获取其标签?

为什么我的 Gtk.Notebook 不能正确获取其标签?

我正在使用此方法保存文件:

def save(self):
    file_data = ConfigParser.ConfigParser()
    subjects_number = self.subjects.get_n_pages()
    si = range(1, subjects_number)
    for sn in si:
        subject_content = self.subjects.get_nth_page(sn)
        subject_name = self.subjects.get_tab_label(subject_content)
        file_data.add_section(subject_name) #Line 153
        pages_number = self.subjects.get_nth_page(sn)
        pi = range(1, pages_number)
        for pn in pi:
            page_content = subject_content.get_nth_page(pn)
            page_name = pages.get_tab_label(page_content)
            c_buffer = page_content.get_buffer()
            c_format = c_buffer.register_serialize_tagset()
            serial = c_buffer.serialize(c_format, c_buffer.get_start_iter(), c_buffer.get_end_iter())
            file_data.set(subject_name, page_name, serial)
    with open(self.current_file, "wb") as this_file:
        file_data.write(this_file)
    print "File Saved:" + self.current_file

然而运行时我在终端窗口中得到了这个回溯:

Traceback (most recent call last):
  File "/home/nja/notetaker/notetaker/NotetakerWindow.py", line 251, in on_btn_save_clicked
    self.save()
  File "/home/nja/notetaker/notetaker/NotetakerWindow.py", line 153, in save
    file_data.add_section(subject_name)
  File "/usr/lib/python2.7/ConfigParser.py", line 260, in add_section
    if section.lower() == "default":
AttributeError: 'Label' object has no attribute 'lower'

这些方法在小部件中创建选项卡Gtk.Notebook()

def add_page(self, pages = None, label = None):
    if pages is None:
        pages = self.get_pages()
    if label is None:
        label = "New Page"
    textview = Gtk.TextView()
    textbuffer = textview.get_buffer()
    pages.append_page(textview, Gtk.Label(label))
    self.window.show_all()

def add_subject(self, label = None):
    if label is None:
        label = "New Subject"
    pages = Gtk.Notebook()
    pages.set_tab_pos(Gtk.PositionType.RIGHT)
    pages.set_scrollable(True)
    self.add_page(pages)
    self.subjects.append_page(pages, Gtk.Label(label))
    self.window.show_all()

我需要做什么才能解决这个错误?

答案1

self.subjects.get_tab_label(subject_content)获取一个 Gtk.Label 实例但file_data.add_section需要一个字符串作为参数。

使用

subject_name = self.subjects.get_tab_label(subject_content).get_text()

相关内容