How to make GtkTextView look like GtkEntry?
Asked Answered
B

4

16

Or "How to add a visible (thin) border to GtkTextView"? Is it ever possible?

Thank you in advance.

Baty answered 5/6, 2011 at 0:22 Comment(3)
Why would you want to confuse your users like that?Specie
@Specie I need the same thing. It is not to confuse the user, but to not confuse the user. I want a 3-line text widget that behaves exactly like an Entry widget (but with 3 lines). The only way I could find is to modify a TextView widget...Pridgen
@Specie Users will be confused without this (even I became confused).Taeniasis
T
9

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 )
Thickness answered 11/6, 2017 at 14:57 Comment(1)
There is no need for a GtkFrame; GtkScrolledWindow is enough.Taeniasis
T
2

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.

Taeniasis answered 23/11, 2020 at 12:12 Comment(1)
Interesting workaround. IMO it would be simpler if the GTK devs just add support for that as-is, but as a work around it is ok; I was thinking of using a gtk-frame. Good to know a scrolled-window can be used as well.Electrocorticogram
P
0

Using Glade editor:

  • In glade editor select your ScrolledWindow (you have packed TextView into ScrolledWindow, haven't you? :), if not - select your TextView).
  • Select Widget Porperties -> Common tab.
  • Find and adjust Border width property to your liking.

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.

Permeate answered 6/6, 2011 at 11:35 Comment(0)
I
0

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.

Indict answered 31/7, 2013 at 18:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.