我想这样显示我的行:
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
最简单的方法是将标签配置为使用固定字体。
如果需要比例字体,则可以将它们分成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像素的空间,则可能需要进行其他检查或更智能的算法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句