Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Tkinter Grid doesn't work properly

I am trying to arrange a logo at the top and widgets in 3 part below the logo. I tried to set the ratio of those 3 part to be same, but when I run the code below, the ratio isn’t same.

My code is

import tkinter as tk

window = tk.Tk()
window.title("My Program")
window.geometry("700x400")

logo = tk.Label(window, text="Logo")
logo.grid(row=0, column=0, columnspan=3, sticky="nw", padx=10, pady=10)

window.columnconfigure(0, weight=1)
window.columnconfigure(1, weight=1)
window.columnconfigure(2, weight=1)

window.rowconfigure(1, weight=1)
window.rowconfigure(2, weight=1)
window.rowconfigure(3, weight=1)

button1 = tk.Button(window, text="Button 1")
button1.grid(row=1, column=0, sticky="nsew", padx=10, pady=10)

button2 = tk.Button(window, text="Button 2")
button2.grid(row=2, column=0, sticky="nsew", padx=10, pady=10)

button3 = tk.Button(window, text="Button 3")
button3.grid(row=3, column=0, sticky="nsew", padx=10, pady=10)

big_label = tk.Label(window, text="Big Label")
big_label.grid(row=1, column=1, rowspan=3, sticky="nsew", padx=10, pady=10)

button4 = tk.Button(window, text="Button 4")
button4.grid(row=1, column=2, sticky="nsew", padx=10, pady=10)

button5 = tk.Button(window, text="Button 5")
button5.grid(row=2, column=2, sticky="nsew", padx=10, pady=10)

entry = tk.Entry(window)
entry.grid(row=3, column=2, sticky="nsew", padx=10, pady=10)

window.mainloop()

When I run the code, the result is below
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

As you can see, the column 2 is thicker than others. How should I fix this?

>Solution :

If you want all the columns have same size, you need to set uniform=1 in all .columnconfigure(...) as well:

window.columnconfigure(0, weight=1, uniform=1)
window.columnconfigure(1, weight=1, uniform=1)
window.columnconfigure(2, weight=1, uniform=1)

Same for all .rowconfigure(...).

Result:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading