简体   繁体   English

Python Tkinter:在同一框架的相对侧放置两个标签

[英]Python Tkinter: place two labels on opposite side of the same frame

in Tkinter I have a frame that fills the X-dimension of my root window. 在Tkinter中,我有一个框架可以填充我的根窗口的X维度。 Inside this frame I want to place two labels: one on the left side and one on the right side. 在此框架内,我想放置两个标签:一个标签在左侧,一个标签在右侧。 However it seems I am still using grid and sticky the wrong way and need your advice: 但是,似乎我仍在使用gridsticky错了方向,需要您的建议:

from Tkinter import *

root = Tk()
root.geometry("250x200")

myFrame = Frame(root, bd=1, relief="sunken")
myFrame.pack(fill=X, padx=10)

myLabel1 = Label(myFrame, text="left").grid(sticky=W)
myLabel2 = Label(myFrame, text="right").grid(sticky=E)

root.mainloop()

This code places the two labels on the left side but on top of each other. 此代码将两个标签放置在左侧但彼此重叠。 If I place them both into the same row 如果我将它们放在同一行

myLabel1 = Label(myFrame, text="left").grid(row=0, sticky=W)
myLabel2 = Label(myFrame, text="right").grid(row=0, sticky=E)

they are at the exact same position, so that myLabel2 covers myLabel1. 它们位于完全相同的位置,因此myLabel2覆盖了myLabel1。

If I do 如果我做

myLabel1 = Label(myFrame, text="left").grid(row=0, column=0, sticky=W)
myLabel2 = Label(myFrame, text="right").grid(row=0, column=1, sticky=E)

then myLabel2 is indeed right of myLabel1, but not at the right side of the frame (although the frame is not resized). 那么myLabel2确实位于myLabel1的右侧,但不在框架的右侧(尽管框架没有调整大小)。

I guess the actual question is: how do I get grid to expand to the whole frame (in x-dimension), so that sticky=E means indeed "right side of the frame" and not "right side of the first column which is as wide as myLabel1". 我猜实际的问题是:如何使grid扩展到整个框架(按x维度),所以sticky = E的意思确实是“框架的右侧”,而不是“第一列的右侧”和myLabel1一样宽。

In order to put myLabel2 on the right side of the frame, you need to use myFrame.columnconfigure(1, weight=1) so that the column 1 will expand to fill the frame. 为了将myLabel2放置在框架的右侧,您需要使用myFrame.columnconfigure(1, weight=1)以便第1列将展开以填充框架。

If you do 如果你这样做

myFrame.columnconfigure(0, weight=1)
myFrame.columnconfigure(1, weight=1)

then each column will take half the frame, but you can also specify different weights to have one column bigger than the other. 那么每列将占用一半的帧,但是您也可以指定不同的权重,以使一列比另一列大。 The columns for which you have not specified any weight adjust their width to fit their content. 您未指定任何权重的列会调整其宽度以适合其内容。

The method rowconfigure does exactly the same but for rows. 方法rowconfigure功能完全相同,只是针对行。

I guess the actual question is: how do I get grid to expand to the whole frame (in x-dimension), so that sticky=E means indeed "right side of the frame" and not "right side of the first column which is as wide as myLabel1" 我猜实际的问题是:如何使网格扩展到整个框架(按x维度),所以sticky = E的意思确实是“框架的右侧”,而不是“第一列的右侧”和myLabel1一样宽

The short answer to that is to make sure that within a frame, at least one column has a non-zero weight. 对此的简短答案是确保一帧内至少一列的权重为非零。 The weight attribute of a column tells tkinter how to distribute any extra space. 列的weight属性告诉tkinter如何分配额外的空间。 The default is zero, meaning that a column won't get any extra space. 默认值为零,这意味着一列将不会获得任何额外的空间。 The extra space goes unused to the right and below the last column and row. 多余的空间将保留在最后一列和每一行的右下方。

note: the weight value can be any positive integer. 注意: weight值可以是任何正整数。 The numbers are relative, so a weight of 2 will create a column that gets twice as much extra space as a column with a weight of 1. 这些数字是相对的,因此权重为2的列将创建一个列,该列的额外空间是权重为1的列的两倍。

As a rule of thumb, you should always designate at least one row and one column to have a positive weight. 根据经验,应始终至少指定一行和一列以具有正的权重。 Usually this is the "hero" widget (typically a canvas or text widget), but it can also be an empty row or column used as a spacer. 通常,这是“英雄”窗口小部件(通常是画布或文本窗口小部件),但也可以是用作分隔符的空行或列。

Layout is highly dependent on everything that is in a container. 布局高度依赖于容器中的所有内容。 If you are only ever going to have just the two labels, I recommend creating a three column configuration. 如果只用两个标签,则建议创建三列配置。 myLabel1 can go in column 0, myLabel2 can go in column 2, and you can configure column 1 to be empty and to fill all of the empty space myLabel1可以进入第0列, myLabel2可以进入第2列,并且您可以将第1列配置为空并填充所有空白空间

myLabel1 = Label(...)
myLabel2 = Label(...)

myLabel1.grid(row=0, column=0, sticky="w")
myLabel2.grid(row=0, column=2, sticky="e")

myFrame.grid_columnconfigure(1, weight=1)
myFrame.grid_rowconfigure(0, weight=1)

Another option would be to go with just two columns, and let each column have an equal weight so all extra spaces is evenly divided between the two columns. 另一种选择是只使用两列,并让每一列具有相等的权重,以便所有多余的空间在两列之间平均分配。

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

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