如何在 TCL/TK 中将标签向左对齐并允许调整大小

问题描述 投票:0回答:1

我想垂直堆叠一组标签,并且希望它们左对齐。

我试过这个:

#!/usr/bin/env tclsh

package require Tk

# Initialize global counter
set widgetCounter 0

# Procedure to generate a unique ID
proc widgetId {prefix} {
    global widgetCounter
    set uniqueId "${prefix}${widgetCounter}"
    incr widgetCounter
    return $uniqueId
}
proc labelId {} { return [widgetId ".lbl"] }
proc sepId {} { return [widgetId ".sep"] }
proc frameId {} { return [widgetId ".frame"] }

# Main GUI Layout
wm title . "WINDOW TITLE"

# 1. HEADER

set l [labelId]
label $l -text "LABEL 1 FOO THIS IS A HEADER" -justify left -fg white -bg darkred -padx 5 -pady 5 
pack $l -side top -anchor w -fill x 

set l [labelId]
label $l -text "ANOTHER LABEL" -justify left -bg blue -padx 5 -pady 5 
pack $l -side top -anchor w -fill x 

set s [sepId]
ttk::separator $s -orient horizontal
pack $s -side top -anchor w -fill x

# Start the application
tkwait window .

但是结果是文本没有左对齐。据我了解,标签正在水平扩展:

tcl tk-toolkit
1个回答
0
投票

您需要将

-anchor w
传递给标签函数,打包锚点和标签锚点是不同的。另外,只有当您有 >1 行时,证明才起作用。

© www.soinside.com 2019 - 2024. All rights reserved.