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

时间:2017-07-31 18:01:00

标签: python-3.x gtk3 gtkentry gtktextview

我一直在使用Gtk Entry,它提供了set_placeholder_text方法来设置占位符文本,但是在文档中,我没有找到TextView的这种方法。

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

1 个答案:

答案 0 :(得分:1)

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

示例:

  1. 将占位符文本设置为变量
  2. 将占位符文本设置为默认的树视图文本缓冲区 含量
  3. 焦点在于,如果文本缓冲区等于占位符,则表示否 以前的输入,所以删除文本(占位符文本)。除此以外 保留内容。
  4. 在焦点输出时,如果文本缓冲区没有文本,则设置内容 作为占位符文本。否则什么都不做。
  5. 这个想法是,如果没有文本或现有文本是占位符文本,则意味着没有用户输入。

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

    产生的用户界面:

    enter image description here

    添加了一些小部件,以便将重点放在其他小部件上。

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