找到另一个锚的位置而不是已经使用的锚 [英] Find position of another anchor than the anchor already used

查看:73
本文介绍了找到另一个锚的位置而不是已经使用的锚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个代码来显示我的问题.它看起来像这样:

I have created a code to show my problem. It looks like this:

userNameID = tkinter.Label(root, text="Name/ID")
userNameID.place(x=640, y=320, anchor="e")

userNameID = tkinter.Label(root, text="Password")
userNameID.place(x=640, y=360, anchor="e")

entButton = tkinter.Button(root, text="Enter", command=enterPrint)
entButton.place(x=640, y=400, anchor="w")

问题是我在中心的一个尺寸上对齐了一些文本,在另一侧对齐了一个按钮.我想知道文本的西北锚点的位置,以确保所有内容与中心的距离相同并且仍然对齐.

The thing is that I have aligned some text on one size of the center and a button on the other side. I want to know the position of the northwest anchor for the texts to make sure everything is the same distance away from the center and still aligned.

推荐答案

grid()pack() 是精确"的,足以满足大多数需求.您只需要学习如何充分利用这些几何管理器.事实上,place() 的使用几乎从来都不是你所需要的.它有它的案例用途,但它不是一般 GUI 设置的好选择.更新代码也很难管理.

grid() and pack() are "precises" enough for most needs. You just need to learn how to properly use these geometry managers to their fullest. In fact the use of place() is almost never what you need. It has its case uses but it is not a great choice for general GUI setups. It is also very hard to manage for updating your code.

更新:在评论中回答您的问题:

Update: To answer you question in the comments:

Row configure 的意思是因为权重你把每一行都设置为 1 个像素?我知道我可以在整个事物上绘制一个网格,但我不知道我的程序有多少行和列.所以我找不到中心.

Row configure means that you put every row to be 1 pixel because of the weight? I know I can draw a grid over the whole thing but I don't know how many rows, columns my program has. therefore I can't find the center.

权重用于告诉特定的行或列应该相对于同一网格/容器内的其他行和列以特定比例调整大小.默认权重为零.当我们将权重设置为 1 时,该行或列将以均匀的速率随窗口调整大小.

The weights are used to tell a specific row or column should resize at a certain ratio in relation to the other rows and columns around them within the same grid/container. The default weight is zero. When we set the weight to 1 then that row or column will resize with the window at an even rate.

以这个示例代码为例:

import tkinter as tk

root=tk.Tk()

center_frame = tk.Frame(root)
center_frame.grid(row=1, column=1, sticky='nsew')

tk.Label(center_frame, text='Name/ID').grid(row=0, column=0)
tk.Label(center_frame, text='Password').grid(row=1, column=0)
tk.Button(center_frame, text='Enter').grid(row=2, column=1)

root.mainloop()

结果:

在上面的示例中,您将看到调整窗口大小时小部件不会移动.这是因为我们没有指示 tkinter 允许任何行或列移动.

In the above example you will see that the widgets do not move when you resize the window. This is because we have not instructed tkinter to allow any rows or columns to move.

如果我们把这个权重加到程序中root.rowconfigure(0, weight=1):

If we add this weight to the program root.rowconfigure(0, weight=1):

然后我们得到这个效果:

Then we get this affect:

接下来添加这个root.rowconfigure(2, weight=1):

接下来添加这个root.columnconfigure(0, weight=1):

接下来添加这个root.columnconfigure(2, weight=1):

正如您在上面的图片中看到的,根据您设置的行和列的权重,行和列将相应地调整大小.我们用上面的代码实现的是调整第 0 行和第 2 行的大小,调整第 0 和 2 列的大小.row 1 column 1 不会调整大小.所以这意味着我们放置在 row 1 column 1 位置的任何东西都不会调整大小,而是保持其与边框的相对位置.因为我们在 row 1 column 1 位置设置了一个框架小部件,然后我们在框架内的所有小部件我们已经有效地使 row 1 column 1 成为中心.

As you can see with the above images depending on what rows and columns you have set a weight to then the rows and columns will resize accordingly. What we achieve with the above code is to have rows 0 and 2 resize and columns 0 and 2 resize. row 1 column 1 will not resize. So that means anything we place in the row 1 column 1 position will not resize but rather maintain its relative location to the borders. Because we set a frame widget in the row 1 column 1 position and then all of our widgets inside the frame we have effectively made row 1 column 1 the center.

行和列的数量与您告诉它的数量一样多.Tkinter 将根据您所说的内容自动调整总行和列.除此之外,它还将使没有任何内容的行和列的大小为零.这意味着只有包含内容(小部件)的行和列才会真正影响网格的大小.

There are as many rows and columns as you tell it to have. Tkinter will automatically adjust the total rows and columns based on what you say. Adding to that it will also make rows and columns with nothing in them a zero size. That means only rows and columns with content(widgets) will actually affect the size of the grid.

假设我们有 3 行 3 列.根据我们如何填充它,它将改变显示内容的大小.

Say we have 3 rows and 3 columns. Depending on how we fill it it will change the size of whats being displayed.

在此示例中,您看到 X 的任何地方都表明我们在以下行和列中放置了一些内容.

In this example anywhere you see an X shows we put something in the following rows and columns.

在这个例子中,我们在 (0,0)、(1,1) 和 (2,2) 中有一些东西.

In this example we have something in (0,0), (1,1) and (2,2).

                ROW
         0       1       2
     *-------*-------*-------*
     |       |       |       |
   0 |   X   |       |       |
C    |       |       |       |
O    *-------*-------*-------*
L    |       |       |       |
U  1 |       |   X   |       |
M    |       |       |       |
N    *-------*-------*-------*
     |       |       |       |
   2 |       |       |   X   |
     |       |       |       |
     *-------*-------*-------*

在这个例子中,我们在 (0,0) 和 (2,2) 中有一些东西.

In this example we have something in (0,0) and (2,2).

         0       2
     *-------*-------*
     |       |       |
   0 |   X   |       |
     |       |       |
     *-------*-------*
     |       |       |
   2 |       |   X   |
     |       |       |
     *-------*-------*

在这个例子中,我们在 (1,0)、(2,1) 和 (2,2) 中有一些东西.

In this example we have something in (1,0), (2,1) and (2,2).

         0       1       2
     *-------*-------*-------*
     |       |       |       |
   1 |   X   |       |       |
     |       |       |       |
     *-------*-------*-------*
     |       |       |       |
   2 |       |   X   |   X   |
     |       |       |       |
     *-------*-------*-------*

以上示例说明了根据您使用小部件填充的行和网格自动显示的内容.

The above examples illustrate what is automatically shown depending on what row and grid you fill with widgets.

至于像 sticky 这样的参数,它们用于管理每个小部件的行为.例如,如果我们告诉小部件填充给定行/列中的所有空间,那么我们将使用 sticky='nsew'.如果我们希望小部件在给定的行/列中水平而不是垂直地拉伸,我们将使用 sticky='ew' 表示东/西.请记住,粘性参数是针对 grid() 的.pack() 几何管理器有它自己的参数.

As for the arguments like sticky these are used to manages behavior of each widget. For example if we tell a widget to fill all the space in a given row/column then we would use sticky='nsew'. If we want a widget to stretch in a given row/column horizontally but not vertically we would use sticky='ew' for east/west. Keep in mind the sticky argument is for grid(). The pack() geometry manager has it own arguments.

有很多选项,您可以在此处找到它们的列表及其说明:

There are many options and you can find a list of them here with their descriptions:

对于grid()

对于pack()

以下面的例子为例.即使您调整窗口大小,此代码也将始终设置窗口的标签和按钮中心.这比放置更容易管理.

Take the below example. This code will set up the labels and button cent of the window at all times even when you resize the window. This is far easier to manage than place.

您可以在几何管理器中使用许多有用的参数来实现您希望的任何行为或对齐.花点时间深入研究一下.我知道 place() 感觉是最好的选择(在 Bryan 纠正这个想法之前,我第一次开始时也这​​么认为:D)但事实并非如此.

There are many useful arguments you can use in your geometry managers to achieve just about any behavior or alignment you wish. Just take the time to dig into it. I know place() feels like the best option (I thought the same when I first started before Bryan corrected that notion :D) but it really is not.

import tkinter as tk

root=tk.Tk()
root.rowconfigure(0, weight=1)
root.rowconfigure(2, weight=1)
root.columnconfigure(0, weight=1)
root.columnconfigure(2, weight=1)

center_frame = tk.Frame(root)
center_frame.grid(row=1, column=1, sticky='nsew')

tk.Label(center_frame, text='Name/ID').grid(row=0, column=0)
tk.Label(center_frame, text='Password').grid(row=1, column=0)
tk.Button(center_frame, text='Enter').grid(row=2, column=1)

root.mainloop()

这是一个使用 pack() 的例子.我发现 grid() 是我的首选,但 pack() 也可以用来完成很多事情.

Here is an example using pack(). I find grid() to be my go to but pack() can be used to accomplish much as well.

import tkinter as tk

root=tk.Tk()

center_frame = tk.Frame(root)
center_frame.pack(expand=True)

tk.Label(center_frame, text='Name/ID').pack(side='top')
tk.Label(center_frame, text='Password').pack(side='top')
tk.Button(center_frame, text='Enter').pack(side='right')

root.mainloop()

这篇关于找到另一个锚的位置而不是已经使用的锚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆