在Gtk.TextView中使用标记 [英] Using markup with Gtk.TextView

查看:67
本文介绍了在Gtk.TextView中使用标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试找到在 Python GObject 中将Pango标记与Gtk.TextViewGtk.TextBuffer一起使用的方法,但似乎此功能仅适用于工具提示和标签. Gtk.TextBuffer具有 方法,但是需要Gtk.TextIter作为输入,并且必须具有指定的长度.

这里的问题是我只想使用Gtk.TextView来显示文本,而不能对其进行编辑.因此,尽管我认为我理解您如何将Gtk.TextTag与可编辑/可选文本一起使用以实质性地更改其外观(甚至行为),但我不确定完全如何处理静态文本.对此应用标记的最简单方法是什么?

例如:将< b>某些文本</b>"转换为"某些文本"(或使用任何标记)

解决方案

Python GTK + 3教程中有一个很好的例子,

,然后可以将标签应用于要加粗的文本部分,为此必须向TextBuffer.apply_tag()方法提供该部分的边界(开始,结束)文字,例如:

start, end = self.textbuffer.get_selection_bounds()
self.textbuffer.apply_tag(self.tag_bold, start, end)

您将全部准备就绪.

在上面的示例中,边界是由用户选择的文本部分确定的,但是,如果您要显示只读文本,则可以在代码中自行提供边界,请查看TextBuffer文档. /p>

您还可以通过以下方法添加具有有效pango标记的文本:

self.textbuffer.insert_markup(iter, markup)

如果该方法可以返回指向插入文本末尾的新iter,那将是很不错的选择,这将使工作变得更加轻松,但是该方法来自自省的内省,因此需要进行覆盖才能实现该目的. /p>

请参见下面的最小示例(您可以使其变得更好):

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

class TextViewWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="TextView Example")

        self.set_default_size(-1, 350)

        self.grid = Gtk.Grid()
        self.add(self.grid)

        self.create_textview()

    def create_textview(self):
        scrolledwindow = Gtk.ScrolledWindow()
        scrolledwindow.set_hexpand(True)
        scrolledwindow.set_vexpand(True)
        self.grid.attach(scrolledwindow, 0, 1, 3, 1)

        self.textview = Gtk.TextView()
        self.textbuffer = self.textview.get_buffer()
        start_iter = self.textbuffer.get_start_iter()

        self.textbuffer.insert(start_iter, "This is some text ")
        self.textbuffer.insert_markup(self.textbuffer.get_end_iter(), "<b>and some bold text</b>", -1)

        scrolledwindow.add(self.textview)

win = TextViewWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

I have been trying to find a way to use Pango markup with Gtk.TextView and Gtk.TextBuffer in Python GObject, but it seems like this functionality is only available for tooltips and labels. Gtk.TextBuffer has the insert_markup method, but requires Gtk.TextIter as input, and must be of specified length.

The issue here is that I want to use Gtk.TextView only for displaying text without it being editable. So while I think I understood how you use Gtk.TextTag with editable/selectable text to change its appearance (and even behaviour) substantially, I am not entirely sure how you do the same with static text. What is the simplest way of applying markup to such?

For example: turning "<b>Some text</b>" into "Some text" (or whichever tags would be used)

解决方案

There is a nice example in the Python GTK+ 3 Tutorial, TextView Example

But to make it clearer (hopefully) on the important parts, as you have guessed you have to use text tags, you have to define those in the TextBuffer, not in the TextView, e.g.

self.tag_bold = self.textbuffer.create_tag("bold", weight=Pango.Weight.BOLD)

and then you can apply your tag to the portion of text that you want to make bold, in order to do that you will have to provide to the TextBuffer.apply_tag() method the bounds (start, end) of that portion of text, like:

start, end = self.textbuffer.get_selection_bounds()
self.textbuffer.apply_tag(self.tag_bold, start, end)

and you will be all set.

In the above example the bounds are taken by the portion of text selected by the user, but of course if you are displaying a read-only text you can provide the bounds yourself in the code, look at the TextBuffer documentation.

You can also add the text with valid pango markup by the method:

self.textbuffer.insert_markup(iter, markup)

Would be nice if the method could return the new iter pointing at the end of the inserted text, which would makes life a lot easier, but the method comes from plain introspection, it would require an override to act like that.

See the minimal example here below (you can make it way nicer):

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

class TextViewWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="TextView Example")

        self.set_default_size(-1, 350)

        self.grid = Gtk.Grid()
        self.add(self.grid)

        self.create_textview()

    def create_textview(self):
        scrolledwindow = Gtk.ScrolledWindow()
        scrolledwindow.set_hexpand(True)
        scrolledwindow.set_vexpand(True)
        self.grid.attach(scrolledwindow, 0, 1, 3, 1)

        self.textview = Gtk.TextView()
        self.textbuffer = self.textview.get_buffer()
        start_iter = self.textbuffer.get_start_iter()

        self.textbuffer.insert(start_iter, "This is some text ")
        self.textbuffer.insert_markup(self.textbuffer.get_end_iter(), "<b>and some bold text</b>", -1)

        scrolledwindow.add(self.textview)

win = TextViewWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

这篇关于在Gtk.TextView中使用标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆