PyGtk3,Label Widget,有没有获取标签文本标记字符串的方法?

PyGtk3, Label Widget, is there a method for get the markup string of label text?

您好,我正在尝试获取标签文本的标记字符串,但我找不到任何类似 mylabel.get_markup 的方法来执行此操作。我该怎么做 ? 为什么 get_markup 方法不存在?

例子

mylabel = gtk.Label()
mylabel.set_markup("<span foreground = 'italic', style = 'italic'>Blue text</span>")

print mylabel.get_markup() # i know this method not exist

#output: <span foreground = 'italic', style = 'italic'>Blue text</span>

是否有类似get_markup方法的方法?

来自 Gtk.Label 的文档:

get_text():

Fetches the text from a label widget, as displayed on the screen. This does not include any embedded underlines indicating mnemonics or Pango markup.

get_label():

Fetches the text from a label widget including any embedded underlines indicating mnemonics and Pango markup.

正如elya5所说,为了获得标签标记,您可以使用label.get_label()方法来完成任务。

以下是您如何操作的示例:

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

class MarkupText(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_title("Test Markup")
        self.set_default_size(300,400)
        self.connect("delete-event", Gtk.main_quit)
        self.label = Gtk.Label()
        button = Gtk.Button()
        button.set_label("Click Me")
        button.connect("clicked", self.get_markup)
        box = Gtk.Box()
        box.pack_start(button, False, False, False)
        box.pack_start(self.label, False, False, False)
        self.add(box)
        self.show_all()
        Gtk.main()

    def get_markup(self, widget):
        a = ["<b>Hello</b>", "<i>Hi</i>", "<b><i>hoo</i></b>"]
        from random import choice
        self.label.set_markup(choice(a))
        # Print label's markup
        print(self.label.get_label())


if __name__ == '__main__':
    app = MarkupText()

终端输出:

<b>Hello</b>
<i>Hi</i>
<i>Hi</i>
<b><i>hoo</i></b>
<b><i>hoo</i></b>