简体   繁体   English

如何处理网格[Tk]

[英]how to handle the grid [Tk]

I created a grid that contains five labels each label in a row but I want in each row has the same size of previous label least size of one colone 我创建了一个网格,其中包含五个标签,每个标签连续一行,但是我希望每一行都具有与前一个标签相同的大小,至少一个冒号的大小

code : 代码:

label .f0 -text "frame 0"\
    -relief groove \
    -borderwidth 2 \
    -background blue
label .f1 -text "frame 1" \
    -relief groove \
    -borderwidth 2 \
    -background gray
label .f2 -text "frame 2" \
    -relief groove \
    -borderwidth 2 \
    -background green
label .f3 -text "frame 3" \
    -relief groove \
    -borderwidth 2 \
    -background yellow
label .f4  -text "frame 4"\
    -relief groove \
    -borderwidth 2 \
    -background red

grid .f0 -row 1 -column 1 -sticky ewns
grid .f1 -row 2 -column 2 -sticky ewns
grid .f2 -row 3 -column 3 -sticky ewns
grid .f3 -row 4 -column 4 -sticky ew
grid .f4 -row 5 -column 5 -sticky ew

printScr : printScr:

在此处输入图片说明

So my question is how I can filling the space that the arrow indicates with Grid not pack 所以我的问题是如何用网格填充箭头指示的空间

You can use the -columnspan option to grid to specify how many columns to use: 您可以使用-columnspan选项来网格化以指定要使用的列数:

  grid .f0 -row 1 -column 1 -sticky ewns -columnspan 5

References: grid 参考: 网格

Edit: 编辑:

foreach i {0 1 2 3 4} {
  grid columnconfigure . $i -weight 1 -uniform labcol
  grid .f$i -column $i -sticky ew -columnspan [expr {5-$i}]
}

This is similar to Peter's setting of minsize. 这类似于Peter的minsize设置。 It makes all the columns the same width. 它使所有列具有相同的宽度。

( 2nd try ) Add this after your code as in the question: 第二次尝试 )将其添加到您的代码后,如问题所示:

foreach index {1 2 3 4 5} {
    grid columnconfigure . $index -minsize [winfo width .f0]
}

grid configure .f0 -columnspan 5
grid configure .f1 -columnspan 4
grid configure .f2 -columnspan 3
grid configure .f3 -columnspan 2

Documentation: foreach , grid , winfo 文档: foreachgridwinfo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM