为什么我无法在 windows 上修复 Gridview 中的框对齐

Why i can't fix the boxes alignment in Gridview on windows

结果之间存在一些 space,正如您在图像中看到的那样,密码输入和生成密码没有对齐

*Result image

from tkinter import 
window = Tk()
window.title("Password Manager")
window.config(padx=20, pady=20)
canvas = Canvas(width=200, height=200, highlightthickness=0)
password_image = PhotoImage(file="logo.png")

image = canvas.create_image(100, 100, image=password_image)
canvas.itemconfig(image)
canvas.grid(row=0, column=1)

website_label = Label(text="Website")
website_label.grid(row=1, column=0)
Email_username_label = Label(text="Email/Username")
Email_username_label.grid(row=2, column=0)
password_label = Label(text="Password")
password_label.grid(row=3, column=0)


website_input = Entry(width=35)
website_input.grid(row=1, column=1, columnspan=2)
Email_username_input = Entry(width=35)
Email_username_input.grid(row=2,column=1, columnspan=2)
password = Entry(width=21)
password.grid(row=3, column=1)

generate_button = Button(text="Generate Password")
generate_button.grid(row=3, column=2)
add_password_button = Button(text="Add", width=36)
add_password_button.grid(row=4, column=1, columnspan=2)



window.mainloop()

您可以看到密码输入和生成密码中有一些space如何修复它

如评论中所述,使用网格方法只需添加 sticky 即可对齐元素。在这种情况下,我已经删除了您条目中的所有 width 选项和带有 stick='EW' 的按钮以向东和向西展开,或者向小部件所在列的最大左侧和右侧展开。

from tkinter import *

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


# Canvas
canvas = Canvas(width=200, height=200, highlightthickness=0)
password_image = PhotoImage(file="logo.png")
image = canvas.create_image(100, 100, image=password_image)
canvas.itemconfig(image)
canvas.grid(row=0, column=1)

# Labels
website_label = Label(text="Website")
website_label.grid(row=1, column=0)
Email_username_label = Label(text="Email/Username")
Email_username_label.grid(row=2, column=0)
password_label = Label(text="Password")
password_label.grid(row=3, column=0)

# Entries
website_input = Entry()
website_input.grid(row=1, column=1, columnspan=2, sticky='EW')  # sticky
Email_username_input = Entry()
Email_username_input.grid(row=2, column=1, columnspan=2, sticky='EW')  # sticky
password = Entry()
password.grid(row=3, column=1, sticky='EW')  # sticky

# Buttons
generate_button = Button(text="Generate Password")
generate_button.grid(row=3, column=2)
add_password_button = Button(text="Add")
add_password_button.grid(row=4, column=1, columnspan=2, sticky='EW')  # sticky

window.mainloop()

如果您希望 GUI 动态调整大小,您可能还需要考虑使用 columnconfigurerowconfigure 阅读 column and row weights