如何在 Gtk.TextView 中设置占位符文本

How to set Placeholder text in Gtk.TextView

到目前为止,我一直在使用 Gtk Entry,它提供了 set_placeholder_text 方法来在其中设置占位符文本,但是,查看文档,我发现 TextView 没有这样的方法。

有什么方法可以在 Gtk Textview 中设置占位符文本吗?

我认为有一个问题与这个几乎相似。这个想法是,例如,使用 focus-in-eventfocus-out-event 检查文本缓冲区内容。

示例:

  1. 将占位符文本设置为变量
  2. 将占位符文本设置为默认的树视图文本缓冲区 内容
  3. On focus in,如果文本缓冲区等于占位符,表示没有 以前的输入,所以删除文本(占位符文本)。除此以外 保留内容。
  4. 在聚焦时,如果文本缓冲区没有文本则设置内容 作为占位符文本。否则什么也不做。

想法是,如果没有文本或现有文本是占位符文本,则表示没有用户输入。

Python 例子

Glade ui 文件(保存为占位符-textview.ui):

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkTextBuffer" id="textbuffer1">
    <property name="text" translatable="yes">Please input text here...</property>
  </object>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkButton">
            <property name="label" translatable="yes">button</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkEntry">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="placeholder_text" translatable="yes">This is an entry and below is a textview</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkScrolledWindow">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="shadow_type">in</property>
            <child>
              <object class="GtkTextView" id="textview1">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="vexpand">False</property>
                <property name="buffer">textbuffer1</property>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">2</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

Python代码:

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


def onFocusIn(self, event):
    if (textbuf.get_text(textbuf.get_start_iter(), textbuf.get_end_iter(), True) == placeholderStr):
        textbuf.set_text("")
    return False


def onFocusOut(self, event):
    if (textbuf.get_text(textbuf.get_start_iter(), textbuf.get_end_iter(), True) == ""):
        textbuf.set_text(placeholderStr)
    return False

placeholderStr = "This is the placeholder text..."

builder = Gtk.Builder()
builder.add_from_file("placeholder-textview.ui")

window = builder.get_object("window1")

textbuf = builder.get_object("textbuffer1")
textbuf.set_text(placeholderStr)

textview = builder.get_object("textview1")
textview.connect("focus-in-event", onFocusIn)
textview.connect("focus-out-event", onFocusOut)

window.connect ("destroy", Gtk.main_quit)
window.show_all()

Gtk.main()

结果UI:

添加了一些小部件以保持对其他小部件的初始关注。

将行为与 Gtk.Entry 进行比较。很像。