I am running Tkinter on Widows in grid layout. The program has a mix of elements with and without columnspan. The elements that span across columns look as expected. But elements (Entry and Button) without columnspan render shifted to the right. Setting padx = 0 does nothing.
window = Tk()
window.title('Password Manager')
window.config(padx=40, pady=40)
current_dir = os.path.dirname(os.path.realpath(__file__))
image_path = f'{current_dir}/logo.png'
logo_img = PhotoImage(file=image_path)
canvas = Canvas(width=200, height=200, highlightthickness=0)
canvas.create_image(100, 100, image=logo_img)
canvas.grid(column=1, row=0)
website_label = Label(text='Website:')
website_label.grid(column=0, row=1)
password_label = Label(text='Password:')
password_label.grid(column=0, row=3)
website_textbox = Entry(width=45)
website_textbox.grid(column=1, row=1, columnspan=2)
password_textbox = Entry(width=21)
password_textbox.grid(column=1, row=3, padx=0)
generate_btn = Button(text='Generate Password', command=generate)
generate_btn.grid(column=2, row=3, padx=0)
window.mainloop()
You don't need to do with `pady.
Here Tkinter Grid is tutorial link.
You merely needed to add two columnspan=2
and three sticky='w'
Add this:
canvas.grid(column=1, row=0, columnspan=2)
website_textbox.grid(column=1, row=1, columnspan=2, sticky='w')
password_textbox.grid(column=1, row=3, pady=0, sticky='w')
generate_btn.grid(column=2, row=3, sticky='w')
Screenshot: