Reputation: 732
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")
How do I prevent the grid from changing size when the button is added ?
Upvotes: 2
Views: 4277
Reputation: 385980
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.
Upvotes: 3