gpt4 book ai didi

Unexpected grid behavior in tkinter - elements aligned incorrectly(Tkinter中的意外网格行为-元素未正确对齐)

转载 作者:bug小助手 更新时间:2023-10-27 19:48:47 26 4
gpt4 key购买 nike



I'm writing a simple UI with Tkinter and can't seem to find a mistake.

我正在用Tkinter编写一个简单的用户界面,似乎找不到错误。


Here is the outcome:
enter image description here

以下是结果:


And here is the code:

代码是这样的:


window = tkinter.Tk()
window.title("Password Manager")
window.configure(padx=20, pady=20)

canvas = tkinter.Canvas(window)
canvas.configure(width=200, height=200)
image = tkinter.PhotoImage(file="logo.png")
canvas.create_image(100, 100, image=image)
canvas.grid(row=0, column=1)

website_label = tkinter.Label(text="Website:")
website_label.grid(row=1, column=0)

website_input = tkinter.Entry(width=35)
website_input.grid(row=1, column=1, columnspan=2)

email_username_label = tkinter.Label(text="Email/Username:")
email_username_label.grid(row=2, column=0)

email_username_input = tkinter.Entry(width=35)
email_username_input.grid(row=2, column=1, columnspan=2)

password_label = tkinter.Label(text="Password:")
password_label.grid(row=3, column=0)

password_input = tkinter.Entry(width=21)
password_input.grid(row=3, column=1)

generate_password_button = tkinter.Button(text="Generate Password")
generate_password_button.config(width=14)
generate_password_button.grid(row=3, column=2)

add_button = tkinter.Button(text="Add", width=21)
add_button.grid(row=4, column=1, columnspan=2)

window.mainloop()

I expected my inputs and buttons to be correctly aligned, i.e. password input to go under upper inputs and generate password button to go right next, fitted to the end of upper inputs.

我希望我的输入和按钮正确对齐,即密码输入放在上面的输入下面,生成密码按钮放在右边,安装在上面的输入的结尾。


I also expected Add button to start in the beginning of the upper inputs.

我还期望添加按钮开始于上层输入的开始。


What am I missing?

我遗漏了什么?


更多回答
优秀答案推荐

By default, when using grid the widgets will be centered in the row and column. That is what is happening here.

默认情况下,当使用网格时,小部件将在行和列中居中。这就是这里正在发生的事情。


The simplest solution is to add the sticky attribute to each widget. That can be used to get the sides of a widget to "stick" to the sides of the row or column.

最简单的解决方案是将Sticky属性添加到每个小部件。这可以用来让小部件的侧面“粘”到行或列的侧面。


For example, add sticky="ew" in the following statements (where "ew" stands for "east, west"):

例如,在以下语句中添加Sticky=“ew”(其中“ew”代表“East,West”):


website_input.grid(row=1, column=1, columnspan=2, sticky="ew")
email_username_input.grid(row=2, column=1, columnspan=2, sticky="ew")
password_input.grid(row=3, column=1, sticky="ew")

I personally think it's a best practice to always explicitly state the sticky values for every widget.

我个人认为,最好总是明确说明每个小部件的粘性值。


更多回答

I think it would be a good idea to try to establish some kind of canonical about how to use .grid properly. But I'm not qualified to phrase the question for it.

我认为尝试建立一些关于如何正确使用.grid的规范将是一个好主意。但我没有资格为这个问题措辞。

Thank you, Bryan, this worked like charm. Don't know why the course I'm taking never mentined it.

谢谢你,布莱恩,这招很管用。不知道为什么我所学的课程从来没有想到这一点。

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com