2011-06-05 48 views
9

或“如何将可见(薄)边框添加到GtkTextView”?它有可能吗?如何使GtkTextView看起来像GtkEntry?

预先感谢您。

+0

为什么你想迷惑你的用户喜欢吗? – ptomato 2011-06-07 21:50:02

+1

@ptomato我需要同样的事情。这不是混淆用户,而是不要混淆用户。我需要一个3行文本小部件,其行为与Entry小部件完全相同(但是有3行)。我能找到的唯一方法是修改一个TextView小部件... – xubuntix 2012-11-11 08:27:52

回答

0

使用格莱德编辑:

  • 在林间空地编辑器中选择您的ScrolledWindow(你已经挤满的TextViewScrolledWindow,不是吗:),如果不是 - 选择的TextView? )。
  • 选择的Widget调性质 - >常见标签。
  • 查找并调整边框宽度属性,以您的喜好。

从代码:

调用容器控件的set_border_width(width)的方法(ScrolledWindow的TextView

注意,在任何情况下文本区将不完全像进入这取决于正在使用的gtk +主题。

0

使用gtk.ScrolledWindow.set_shadow_type(type=gtk.SHADOW_ETCHED_IN)会改善外观,但不符合gtk.Entry的风格。

如果放置在一个窗口或窗格中,被击倒的窗口或textview的边界不是问题,但是如果目标是创建一个带有多行输入字段的窗体,它会变得很难看。这里是一个黑客工具,可以做的伎俩......

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

需要说明的是滚动条(S)变得不可见;它可以被选择,并且滚动像往常一样工作。如果要在该字段中输入的数据倾向于不使用滚动,这可能是一个小问题。

6

年后...但在网上搜索仍然没有给这个问题很好的答案。

解决的办法很简单:只需创建一个GtkFrame并添加GtkScrolledWindow包含GtkTextView,这里是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)