当前位置: 动力学知识库 > 问答 > 编程问答 >

tcl - Label inside a frame won't align the same

问题描述:

So I'm making an interface using TCL/TK and I'm having a weird issue, pictured below. The two frames on the left (colored red) are made in the same way. The labels should be stickying to the e and w but as you can see the frame under the first one does not arrange the label in the same way.

I create the frames like this

labelframe .bswSelfTestFrame -text "BSW Self Test Summary"

labelframe .bswStatusFrame -text "BSW General Status"

I pack the frame like this

pack .bswStatusFrame -in .hkframe -padx 3 -pady 3 -anchor n -expand yes -fill both

pack .bswSelfTestFrame -in .hkframe -padx 3 -pady 3 -expand yes -fill both

pack .hkframe -side left -expand no

And the labels

grid .bswStatusFrame.lBSWCurrentMode -in .bswStatusFrame -padx 5 -pady 2 -row 1 -column 1 -sticky w

grid .bswStatusFrame.vBSWCurrentMode -in .bswStatusFrame -padx 10 -pady 2 -row 1 -column 2 -sticky e

grid .bswSelfTestFrame.lLEONRAMtestErr -in .bswSelfTestFrame -padx 5 -pady 2 -row 1 -column 1 -sticky w

grid .bswSelfTestFrame.vLEONRAMtestErr -in .bswSelfTestFrame -padx 10 -pady 2 -row 1 -column 2 -sticky e

网友答案:

The problem is that the grid geometry manager doesn't know what to do with the extra space in the lower widget, so it centers all the cells. (There are gaps to both left and right, but none at the top and bottom here because the overall window is constrained.) This is often not what is really wanted, but it does encourage you to be clear what you want to actually get.

The best fix is to nominate one column to receive the extra space. This column can be either the left or right one, or even a column that has no widgets in it at all. The column that will have to deal with the space should be configured to have a non-zero weight. You can also give two columns non-zero weights, when they'll share the extra space according to their weighting.

# The simplest fix...
grid columnconfigure .bswSelfTestFrame 1 -weight 1
# Repeat for all master widgets where you want to fix things
# The blank-column version...
grid columnconfigure .bswSelfTestFrame 2 -weight 1
# Note that *I HAVE CHANGED THE COLUMN OF ONE LABEL* below
grid .bswSelfTestFrame.lLEONRAMtestErr -in .bswSelfTestFrame -padx 5 -pady 2 -row 1 -column 1 -sticky w
grid .bswSelfTestFrame.vLEONRAMtestErr -in .bswSelfTestFrame -padx 10 -pady 2 -row 1 -column 3 -sticky e
# The two-weighted-column version, both with the same weight...
grid columnconfigure .bswSelfTestFrame 1 -weight 1
grid columnconfigure .bswSelfTestFrame 2 -weight 1

# Alternatively, with new enough version of Tk...
grid columnconfigure .bswSelfTestFrame {1 2} -weight 1

Try them out! Find which works in the best way for you and use that.

分享给朋友:
您可能感兴趣的文章:
随机阅读: