3

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

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

4

1 回答 1

2

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

例子:

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

这个想法是,如果没有文本或现有文本是占位符文本,则意味着没有用户输入。

Python 示例

Glade ui 文件(另存为 placeholder-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()

生成的用户界面:

在此处输入图像描述

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

将行为与 Gtk.Entry 进行比较。它非常相似。

于 2017-08-02T10:33:06.863 回答