Reputation: 13649
Or "How to add a visible (thin) border to GtkTextView"? Is it ever possible?
Thank you in advance.
Upvotes: 16
Views: 4494
Reputation: 1180
After about 9 and a half years later...
I'm going to give a language-independent answer.
First, add a GtkScrolledWindow
, it'll enable scrolling. Now add your GtkTextView
. Then set shadow type to something other than none. It'll show a border around your GtkTextView
.
Upvotes: 2
Reputation: 422
Years later... but searching the web still gives no good answers to this question.
The solution is quite simple: Just create a GtkFrame
and add the GtkScrolledWindow
containing the GtkTextView
, here is some example code in 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 )
Upvotes: 9
Reputation: 895
Using gtk.ScrolledWindow.set_shadow_type(type=gtk.SHADOW_ETCHED_IN)
will improve the look but won't match the style of gtk.Entry
.
The border of the scolled window or textview is not an issue if placed in a window or pane, but if the goal is to create a form with a multi-line entry field it becomes ugly. Here is a hack that may do the trick...
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()
The caveat is that the scrollbar(s) becomes invisible; it can be selected, and scrolling works as usual. This may be a minor issue if the data to be entered in the field tends to not make use of scrolling.
Upvotes: 0
Reputation: 10299
Using Glade editor:
From code:
call set_border_width(width)
method of container widget (either ScrolledWindow or TextView)
Note, that in any case TextArea will not exactly look like Entry and it depends on gtk+ theme being used.
Upvotes: 0