【问题标题】:How to make GtkTextView look like GtkEntry?如何让 GtkTextView 看起来像 GtkEntry?
【发布时间】:2021-01-24 14:25:52
【问题描述】:

或“如何为 GtkTextView 添加可见(细)边框”?有可能吗?

提前谢谢你。

【问题讨论】:

  • 你为什么要这样迷惑你的用户?
  • @ptomato 我需要同样的东西。不是迷惑用户,而是不迷惑用户。我想要一个 3 行文本小部件,其行为与 Entry 小部件完全相同(但有 3 行)。我能找到的唯一方法是修改 TextView 小部件...
  • @ptomato 没有这个用户会感到困惑(甚至我也会感到困惑)。

标签: gtk pygtk glade gtktextview gtkentry


【解决方案1】:

多年后......但在网上搜索仍然没有很好地回答这个问题。

解决方案很简单:只需创建一个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 )

【讨论】:

  • 不需要GtkFrame; GtkScrolledWindow 就够了。
【解决方案2】:

大约 9 年半之后……

我将给出一个与语言无关的答案。

首先,添加一个GtkScrolledWindow,它将启用滚动。现在添加您的GtkTextView。然后将阴影类型设置为非无。它会在您的GtkTextView 周围显示一个边框。

【讨论】:

  • 有趣的解决方法。如果 GTK 开发人员按原样添加对 IMO 的支持会更简单,但作为一种解决方法是可以的;我正在考虑使用 gtk 框架。很高兴知道也可以使用滚动窗口。
【解决方案3】:

使用 Glade 编辑器:

  • 在林间空地编辑器中选择您的 ScrolledWindow(您已将 TextView 打包到 ScrolledWindow 中,不是吗?:),如果没有 - 选择你的 TextView)。
  • 选择 Widget Porperties -> Common 选项卡。
  • 根据自己的喜好查找并调整 边框宽度 属性。

来自代码:

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

请注意,在任何情况下,TextArea 都不完全像 Entry,它取决于所使用的 gtk+ 主题。

【讨论】:

    【解决方案4】:

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

    如果放置在窗口或窗格中,scolled 窗口或 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()
    

    需要注意的是滚动条变得不可见;可以选择它,并且滚动照常工作。如果要在字段中输入的数据往往不使用滚动,这可能是一个小问题。

    【讨论】:

      猜你喜欢
      • 2012-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2015-03-18
      • 1970-01-01
      • 2020-06-13
      相关资源
      最近更新 更多