2

I have a Tkinter class with a grid in it, each of the grid rows should be the same weight as stated in the row configuration and the content of the grid should not affect the grid dimensions as I turned propagation off.

This works fine as long as I have empty frames with background colours in the grid but as soon as I add a button to the top grid row the row increases in size compared to the other ones by about exactly the height of the button itself. The code is as follows:

class MainMenuPage(tkinter.Frame):
def __init__(self,parent,controller):
    tkinter.Frame.__init__(self,parent)
    self.controller=controller

    self.columnconfigure(0,weight=1)
    self.rowconfigure(0,weight=1)
    self.rowconfigure(1,weight=1)
    self.rowconfigure(2,weight=1)
    self.rowconfigure(3,weight=1)
    self.grid_propagate(0);

    self.color0=tkinter.Frame(self,bg="#bd1e2b")
    self.color0.grid(column=0,row=0,sticky="nesw")

    self.color1=tkinter.Frame(self,bg="#1e1ebd")
    self.color1.grid(column=0,row=1,sticky="nesw")

    self.color2=tkinter.Frame(self,bg="#1ebd48")
    self.color2.grid(column=0,row=2,sticky="nesw")

    self.color3=tkinter.Frame(self,bg="#bdb81e")
    self.color3.grid(column=0,row=3,sticky="nesw")

    image=Image.open("/path/settingsIcon.png")
    image=image.resize((54,54),Image.ANTIALIAS)
    self.settingsIcon=ImageTk.PhotoImage(image)
    self.settingsButton=tkinter.Button(self,compound=tkinter.TOP,width=54,height=54,image=self.settingsIcon,command=lambda:self.controller.showPage("valveMain"))
    self.settingsButton.grid(column=0,row=0,sticky="ne")

enter image description here

How do I prevent the grid from changing size when the button is added ?

Mercury
  • 594
  • 1
  • 12
  • 28
  • 1
    Are you wanting to enforce a strict limit, or are you willing to have a solution that resizes the height of all rows if one row changes height? In other words, is the goal "don't let rows resize" or is it "all rows should be the same size"? I don't understand why you would want to force a row height to be smaller than what is in that row. – Bryan Oakley Sep 21 '19 at 18:25
  • That’s the thing, the button isn’t bigger then the row. But I basically do want to enforce strickt sizes, but I don’t want to enforce them in absolute sizes but in ratios(weight) – Mercury Sep 21 '19 at 18:29
  • And I want to make the grid resize relative to their parent and not the content of it – Mercury Sep 21 '19 at 18:30
  • 1
    `weight` isn't for defining relative sizes. It is for defining how to allocate extra space. – Bryan Oakley Sep 21 '19 at 18:35
  • So then how do I get a table where every row is allocated the same height and it isn’t affected by the content – Mercury Sep 21 '19 at 18:37

1 Answers1

3

I don't fully undestand what you're trying to accomplish. If the goal is to make sure that every row and/or column is the same size, you can configure rows and columns to be part of a uniform group. weight alone won't do that because it only configures how extra space is allocated, it doesn't control the absolute size of a row or column.

To create a uniform group, use the uniform option for row_configure and column_configure. All rows or columns in the same uniform group will have the same size in proportion to their weight. If their weight is the same, their size will be the same.

From the canonical tcl/tk documentation:

The -uniform option, when a non-empty value is supplied, places the row in a uniform group with other rows that have the same value for -uniform. The space for rows belonging to a uniform group is allocated so that their sizes are always in strict proportion to their -weight values.

In your case, you can try something like this:

self.rowconfigure(0,weight=1, uniform='row')
self.rowconfigure(1,weight=1, uniform='row')
self.rowconfigure(2,weight=1, uniform='row')
self.rowconfigure(3,weight=1, uniform='row')

For more information see The grid algorithm in the tcl/tk documentation. It describes precisely how the grid algorithm works.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685