harun 2525
harun 2525

Reputation: 47

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

hi i am trying to get markup string of label text but i couldn't find any method like mylabel.get_markup to do this. how can i do it ? why isn't get_markup method exist ?

example

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>

is there a method like get_markup method in exemple ?

Upvotes: 1

Views: 891

Answers (2)

Chiheb Nexus
Chiheb Nexus

Reputation: 9257

As elya5 said, in order to get a label markup you can use label.get_label() method to achieve the task.

Here is an example of how you can do it:

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

Terminal output:

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

Upvotes: 2

elya5
elya5

Reputation: 2258

From the documentation for 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.

Upvotes: 3

Related Questions