如何使GtkTextView看起来像GtkEntry?

16
或者说“如何向GtkTextView添加可见(细)边框”?这是否可能?
先行致谢。

为什么你想要这样混淆用户呢? - ptomato
4
@ptomato 我需要同样的东西。这不是为了混淆用户,而是为了不让用户混淆。我想要一个3行文本小部件,它的行为方式与Entry小部件完全相同(但有3行)。我找到的唯一方法是修改TextView小部件... - xubuntix
1
@ptomato 如果没有这个(即使我自己也感到困惑),用户会感到困惑。 - Akib Azmain Turja
4个回答

9
多年以后... 但在搜索互联网时仍然找不到有关此问题的好答案。
解决方案非常简单:只需创建一个 GtkFrame,并添加包含 GtkTextViewGtkScrolledWindow,以下是一些用Python编写的示例代码:
frame = Gtk.Frame()
scroll = Gtk.ScrolledWindow()
scroll.set_hexpand( True )
scroll.set_border_width( 3 )
textview = Gtk.TextView()
scroll.add( textview )
frame.add( scroll )

1
不需要使用GtkFrame;GtkScrolledWindow就足够了。 - Akib Azmain Turja

2
大约过了9年半之后...
我将给出一份与语言无关的答案。
首先,添加一个 GtkScrolledWindow,它将启用滚动。现在添加你的 GtkTextView。然后将阴影类型设置为非“none”。这将显示围绕你的 GtkTextView 的边框。

有趣的解决方法。在我看来,如果GTK开发人员直接支持这个功能会更简单,但作为一种解决方法也可以;我曾考虑过使用gtk-frame。很高兴知道滚动窗口也可以使用。 - shevy

0

使用 Glade 编辑器:

  • 在 Glade 编辑器中选择您的 ScrolledWindow(您已经将 TextView 打包到 ScrolledWindow 中了吗?如果没有,请选择您的 TextView)。
  • 选择 Widget Porperties -> Common 选项卡。
  • 找到并调整 Border width 属性以符合您的喜好。

从代码中:

调用容器小部件(ScrolledWindowTextView)的 set_border_width(width) 方法。

请注意,无论如何,TextArea 都不会完全像 Entry,这取决于所使用的 gtk+ 主题。


0

使用gtk.ScrolledWindow.set_shadow_type(type=gtk.SHADOW_ETCHED_IN)可以改善外观,但不会与gtk.Entry的样式匹配。

如果将滚动窗口或文本视图放置在窗口或面板中,则边框不是问题,但如果目标是创建带有多行输入字段的表单,则变得丑陋。这里有一个可能起作用的hack...

import gtk

# create an entry widget that we use for appearances only
e=gtk.Entry()
e.set_size_request(width=250, height=150)

# create a texview and accompaying label
lbl = gtk.Label(str="Comments: ")
lbl.set_alignment(xalign=1, yalign=0)
field = gtk.TextView(buffer=None)
field.set_wrap_mode(wrap_mode=gtk.WRAP_WORD) # or gtk.WRAP_CHAR

# we need a scroll window
sw = gtk.ScrolledWindow(hadjustment=None, vadjustment=None)
sw.set_border_width(border_width=4)
sw.set_size_request(width=250, height=150)
sw.set_policy(hscrollbar_policy=gtk.POLICY_NEVER, vscrollbar_policy=gtk.POLICY_AUTOMATIC)
sw.add(field)

# create more widgets as needed for form here...
lbl2 = gtk.Label(str="email: ")
lbl2.set_alignment(xalign=1, yalign=0)
field2 = gtk.Entry()

# put everything in a table so the fields and labels are all aligned 
tbl = gtk.Table(rows=1, columns=2, homogeneous=False)
tbl.attach(lbl, left_attach=0, right_attach=1, top_attach=0, bottom_attach=1, xoptions=gtk.FILL|gtk.SHRINK, yoptions=gtk.FILL, xpadding=0, ypadding=0)
# sw and e must be attached in this order, the reverse will not work
tbl.attach(sw, left_attach=1, right_attach=2, top_attach=0, bottom_attach=1, xoptions=gtk.FILL|gtk.SHRINK, yoptions=gtk.FILL, xpadding=0, ypadding=0)
tbl.attach(e, left_attach=1, right_attach=2, top_attach=0, bottom_attach=1, xoptions=gtk.FILL|gtk.SHRINK, yoptions=gtk.FILL, xpadding=0, ypadding=0)
# comment out previous line to see difference

# attach other widgets here...
tbl.attach(lbl2, left_attach=0, right_attach=1, top_attach=1, bottom_attach=2, xoptions=gtk.FILL|gtk.SHRINK, yoptions=gtk.FILL, xpadding=0, ypadding=0)
tbl.attach(field2, left_attach=1, right_attach=2, top_attach=1, bottom_attach=2, xoptions=gtk.FILL|gtk.SHRINK, yoptions=gtk.FILL, xpadding=0, ypadding=0)

# display it!
window = gtk.Window()
window.set_default_size(350, 200)
window.connect("destroy", lambda w: gtk.main_quit())
window.add(tbl)
window.show_all()

gtk.main()

注意的是,滚动条会变得不可见; 但仍可以被选中,且滚动功能正常。如果要输入的数据不涉及使用滚动,则这可能是一个较小的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接