ttk::label 多行格式
ttk::label multiline formatting
我想这样显示我的台词:
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 中可用的 spaces 进行不同的播放:
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 像素 space。
可能需要额外的检查或更智能的算法
标签小部件并不是真正为这类事情设计的。您可以使用固定宽度的字体,或者尝试插入制表符:
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.
(您需要尝试如何使小部件的整体宽度正确。以及将背景设置为什么颜色。)
我想这样显示我的台词:
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 中可用的 spaces 进行不同的播放:
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 像素 space。
可能需要额外的检查或更智能的算法标签小部件并不是真正为这类事情设计的。您可以使用固定宽度的字体,或者尝试插入制表符:
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.
(您需要尝试如何使小部件的整体宽度正确。以及将背景设置为什么颜色。)