gpt4 book ai didi

linux - 如何在 Tcl/Tk 中自动在标签中换行?

转载 作者:行者123 更新时间:2023-12-03 09:59:21 25 4
gpt4 key购买 nike

有没有人做过或知道如何做一个自动[label ] 越线?还是智能识别到达每一行的末尾,即边缘的边缘,仅限于小部件本身?

代码:

package require Img

# Container
frame .fr -borderwidth 2 -bg white

# Text Variable
set description "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris"

# Thumbnail
image create photo pic -file /home/tc/webvideo/galinha_pitadinha/seu_lobato.jpg
button .fr.bt -relief flat -bg white -image pic

# Description
label .fr.lb -bg white -textvariable description -wraplength 250 -width 30 -justify left

pack .fr .fr.bt .fr.lb -side left -fill x -padx .5c -pady .5c

代码结果:

enter image description here

从编程上讲,我希望它看起来像这样:

enter image description here

我想打破句子,以及上面的说明性图像。

请注意,缺少正则表达式来搜寻 第二行结束最后嫁给小盆(椭圆)。当然,隐藏其余的文本。

将鼠标悬停在 label 上时将显示所有文本小部件通过: tooltip
我一直在思考两个假设。他们是:
  • 1) 显示 Text 变量中“字符”的总数
    tk_messageBox -message [string length $description]
  • 2) 显示 Text 变量中“单词”的总数
    tk_messageBox -message [llength $description]

  • 我停在这里:

    In this setting of the -wraplength 250 property, I have a variation of 10 words for every two lines. Based on this, I can apply a condition and put the amount of word or character as a determining factor to only display up to the second line.


    # If it were to quantify Characters and not words, this would be
    if {[string length $description] < 40} {
    pack [label .b -textvariable description -wraplength 250 -width 30 -justify left] -side top -fill x
    }

    或者比较一定数量的单词,在这种情况下是 10 个单词。如果为真,则执行正则表达式的 Action
     # If it were to quantify words and not characters, this would be
    if {[llength $description] <10} {
    ... RegExp code here ...
    }

    最佳答案

    标签小部件采用 -wraplength option .以任何形式应用以提供屏幕距离度量都需要一个环绕宽度(例如,190 为 190 像素,5c 为 5 厘米)。

    (很少使用的)消息小部件可以改为专注于尝试为其包含的文本获得特定的纵横比。

    如果您愿意破坏性地更改正在显示的文本,则可以在脚本中进行省略号注入(inject)。

    pack [label .l -textvar msg]
    set msg "Lorem ipsum,\nfoo bar grill whatever to make this long"

    proc trimWithEllipsis {msg width font {ellipsis ...}} {
    set ew [font measure $font $ellipsis]
    set out ""
    foreach c [split $msg ""] {
    if {[font measure $font $out$c] > $width} {
    set oute ""
    foreach c2 [split $out ""] {
    if {[font measure $font $oute$c2$ellipsis] > $width} {
    return $oute$ellipsis
    }
    append oute $c2
    }
    # failed to find ellipsis injection point??? keep going normally...
    }
    append out $c
    }
    return $msg
    }

    # How to apply the code
    set msgLines {}
    foreach line [split $msg "\n"] {
    # 200 (pixels) was a good demonstration value on this system
    lappend msgLines [trimWithEllipsis $line 200 [.l cget -font]]
    }
    set msg [join $msgLines "\n"]

    我与其他 Tk 开发人员谈过话,我们一致认为这类事情应该是核心功能,特别是因为它也可以很好地适用于其他小部件类型(尤其是条目和列表框)。这应该是核心功能的另一个原因是,这意味着包装的细节不会成为模型的一部分(即变量/ -message 属性的内容),而纯粹是 View 关注点。但是它没有机会制作 8.6 或更早版本。

    关于linux - 如何在 Tcl/Tk 中自动在标签中换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58601639/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com