0

Using the example from this SO question with a couple slight modifications to the size: What does 'weight' do in tkinter?

import tkinter as tk

root = tk.Tk()
root.geometry("400x200")

f1 = tk.Frame(root, background="bisque, height=200)
f2 = tk.Frame(root, background="pink", height=200)

f1.grid(row=0, column=0, sticky="nsew")
f2.grid(row=0, column=1, sticky="nsew")

root.grid_columnconfigure(0, weight=4)
root.grid_columnconfigure(1, weight=1)


root.mainloop()

This produces the expected output of this:

column f2 takes up a quarter of the screen

If I then add a label to column F2 that is not wider than the already existing column, f2 gets wider anyway. The more characters the worse it gets:

import tkinter as tk

root = tk.Tk()
root.geometry("400x200")

f1 = tk.Frame(root, background="bisque", height=200)
f2 = tk.Frame(root, background="pink", height=200)

f1.grid(row=0, column=0, sticky="nsew")
f2.grid(row=0, column=1, sticky="nsew")

root.grid_columnconfigure(0, weight=4)
root.grid_columnconfigure(1, weight=1)

label_example = tk.Label(f2, font=("Impact", 20, "italic", "bold"), text="A", fg='dark orange', bg='black')
label_example.pack()

root.mainloop()

column f2 is now taking more than a quarter

I don't understand why column f2 is getting wider when I add this label to it. This happens even without setting the font size. It seems like there is padding going on with the label OR that adding things to frames somehow affects the weight property. I have been stumped on this for quite a while now. I understand I can use grid_propagate to disable this, but that seems like a hacky way to fix this. I want to learn /why/ it is happening.

What I need is for one column to take up 3/4 of the screen with the other taking 1/4. What am I not understanding about column width weights? Thanks

iamalion
  • 1
  • 3
  • 2
    `weight=` normally controls the distribution of *excess* space, beyond what is actually needed to hold the widgets. If you want it to strictly set the proportions of column sizes, you also need to add a `uniform=` option for the columns - the value doesn't matter, it just has to be the same for all of them. – jasonharper Sep 08 '21 at 23:54
  • Perfect, that was exactly what I was looking for. Looking up tkinter uniform I came across this similar question: https://stackoverflow.com/questions/45467138/tkinter-columns-of-equal-weight-are-not-equal-width Since I am not looking for equal sized columns I overlooked this one. – iamalion Sep 09 '21 at 00:53

0 Answers0