1

I am trying to add a non-zero weight value to all widgets created in a tkinter aplication with python 3.x. I want to make all the grids in all children frames expand with the root window.

I have this code:

# Gets the size of the roots grid
Grid_Size = root.grid_size()

# Loops through the columns in the grid
for i in range(Grid_Size[0]):
    # Configures a weight of 1 to the column
    root.columnconfigure(i, weight=1)

# Loops through all the rows in the grid
for i in range(Grid_Size[1]):
    # Configures a weight of 1 to the row
    root.rowconfigure(i, weight=1)

However, this only works for the roots first grid not any of the cildrens grids.

My next attempt tried to find all of the items in the root and then configure all those items individually:

# Creats a list of the frames children
child_list = root.winfo_children()

# Iterates though every child in the list
for child in child_list:
    # If the child has children
    if child.winfo_children():
        # Adds the children to the list to be iterated though
        child_list.extend(child.winfo_children())

# Goes through all of the widgets
for child in child_list:

    # Gets the nuber of rows and columns of the grid
    Grid_Size = child.grid_size()

    # Loops through every column
    for i in range(Grid_Size[0]):
        # Sets the weight to a non zero value so it can expand
        child.columnconfigure(i, weight=1)

    # Loops through every row
    for i in range(Grid_Size[1]):
        # Sets the weight to a non zero value so it can expand
        child.rowconfigure(i, weight=1)

However, this simplay doesn't seem to be working. The row and column configure methods are being called, but the widgets are still not expanding with the root window. All the frames and widgets were placed to the root with

widget.grid(row=x, column=y, sticky="nsew")

where x and y are relevant numbers

Elephant
  • 446
  • 1
  • 7
  • 19
  • 1
    _"I am trying to add a non-zero weight value to all widgets"_ - widgets don't have weights, columns and rows have weights. – Bryan Oakley Aug 14 '18 at 16:22
  • You can only apply row and column weights per container. The main root window, the Toplevel windows and Frames. – Mike - SMT Aug 14 '18 at 16:26
  • Your first section of code should work correctly. Are you trying to set some weights to other containers as well? Say inside of a Frame? – Mike - SMT Aug 14 '18 at 16:29
  • setting weights this way is almost always a bad idea. You should the weights in the same block of code that you add widgets. You shouldn't be writing code that requires that all inner widgets use `grid`. – Bryan Oakley Aug 14 '18 at 16:42
  • @BryanOakley I agree with you that the weights should be set when the widgets are set. However this should work. I tested it on my end and I do not see a problem here (with the first block of code in the OPs question that is). I suspect the OP is trying to do the same thing to some other containers and may be using root instead of the container name. – Mike - SMT Aug 14 '18 at 16:47
  • @Mike-SMT: yes, it absolutely should work, assuming all of the assumptions the OP is making is true. Clearly, they are not. There are just too many unknowns for us to give a good answer. – Bryan Oakley Aug 14 '18 at 17:21

0 Answers0