【问题标题】:ttk::label multiline formattingttk::label 多行格式
【发布时间】:2019-11-16 23:16:41
【问题描述】:

我想这样显示我的台词:

Nombre de seq(s)     = 10
Nombre de page(s)    = 12
Fichier word         = c:/temp/word.docx
Fichier simulation   = c:/temp/word.tmp

但在我的标签中,我的行是这样显示的

如何使我的行与等号对齐

在我的代码下面:

package require Tk

lappend info [format "%-20s = %-1s" "Nombre de seq(s)" 10]
lappend info [format "%-20s = %-1s" "Nombre de page(s)" 1]
lappend info [format "%-20s = %-1s" "Fichier word" {c:/temp/word.docx}]
lappend info [format "%-20s = %-1s" "Fichier simulation" {c:/temp/word.tmp}]

grid [ttk::label .l -text [join $info "\n"]] -row 0 -column 0 -padx 2 -pady 2 -sticky nw 

【问题讨论】:

    标签: tcl tk


    【解决方案1】:

    最简单的方法是将标签配置为使用固定字体。

    如果你想要一个比例字体,那么你可以把东西分成 8 个单独的标签,然后把它们放在一个网格中。

    要使用比例字体只有一个标签,您可以使用 unicode 中可用的不同空格:

    set data {
        "Nombre de seq(s)" 10
        "Nombre de page(s)" 12
        "Fichier word" "c:/temp/word.docx"
        "Fichier simulation" "c:/temp/word.tmp"
    }
    
    # Put the different size spaces in a dict
    set spaces [dict create [font measure TkDefaultFont " "] " "]
    for {set i 0x2000} {$i <= 0x200a} {incr i} {
        set s [format %c $i]
        dict set spaces [font measure TkDefaultFont $s] $s
    }
    # A 0-width space could cause an endless loop
    dict unset spaces 0
    # Sort the dict to be able to use a bisect lsearch
    set spaces [lsort -integer -index 0 -stride 2 $spaces]
    
    # Calculate the sizes of the lefthand terms and find the longest
    set sizes [lmap n [dict keys $data] {font measure TkDefaultFont $n}]
    set max [tcl::mathfunc::max {*}$sizes]
    
    set txt [lmap s [dict keys $data] l $sizes {
        set v [dict get $data $s]
        # Keep adding the biggest space that will fit
        # until the string is the same length as the longest one
        while {$l < $max} {
            set w [expr {$max - $l}]
            set key [lsearch -inline -integer -bisect [dict keys $spaces] $w]
            append s [dict get $spaces $key]
            incr l $key
        }
        # Append the value
        append s " = " $v
    }]
    
    # Combine the lines and show them on a label
    ttk::label .l -text [join $txt \n]
    grid .l
    

    如果字体没有任何 1 像素空间,则可能需要额外的检查或更智能的算法。

    【讨论】:

      【解决方案2】:

      标签小部件并不是真正为这类事情设计的。您可以使用固定宽度的字体,也可以尝试插入制表符:

      pack [ttk::label .l -textvariable foo]
      
      # Doing this directly rather than computing it
      set foo "Nombre de seq(s)\t\t= 10
      Nombre de page(s)\t= 12
      Fichier word\t\t= c:/temp/word.docx
      Fichier simulation\t\t= c:/temp/word.tmp"
      

      选项卡在这里有点棘手,因为行长之间的差异对于它们自动隐藏来说有点太大了。

      最好的替代方案可能是使用只读文本小部件,因为该小部件可让您精确控制布局。

      pack [text .t -font TkDefaultFont]
      
      .t insert 1.0 "Nombre de seq(s)\t= 10
      Nombre de page(s)\t= 12
      Fichier word\t= c:/temp/word.docx
      Fichier simulation\t= c:/temp/word.tmp"
      
      .t configure -tabs [font measure TkDefaultFont "Nombre de page(s) "] -state disabled
      # You MUST insert the text before disabling the widget.
      

      (您需要尝试如何正确设置小部件的整体宽度。以及将背景设置为什么颜色。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-04
        • 2017-07-28
        • 2011-08-27
        • 1970-01-01
        • 1970-01-01
        • 2017-11-23
        • 2020-08-05
        • 2021-10-08
        相关资源
        最近更新 更多