我一直在使用Gtk Entry,它提供了set_placeholder_text方法来设置占位符文本,但是在文档中,我没有找到TextView的这种方法。
有什么办法可以在Gtk Textview中设置占位符文本吗?
答案 0 :(得分:1)
我认为有一个问题几乎与此类似。我的想法是,例如,使用focus-in-event
和focus-out-event
来检查文本缓冲区内容。
示例:
这个想法是,如果没有文本或现有文本是占位符文本,则意味着没有用户输入。
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进行比较。它非常相似。