Home > database >  TkInter Making a large number of cells with Grid, while using Geometry property
TkInter Making a large number of cells with Grid, while using Geometry property

Time:01-27

I want to make a lot of rows and columns on Tkinter Grid geometry manager. As you may know, while using the Geometry property to set a specific height and weight to a window, you have to specify rowconfigure and columnconfigure ( Correct me if im wrong. )

I wanted to do this:

for x in range(10):

root.rowconfigure(x, weight=1)

As it is obvious, it just creates a row with weight 1 on the last range number. This should be easy to do, but I dont see how right now. Thanks in advance.

CodePudding user response:

Row and Column configure has nothing to do with your 'geometry', but with the size of the widget you are trying to assemble your grid, if it is the root, then yes it will have a direct connection with the geometry, you are forgetting the 'index ' when calling configuration, here's an example of how it would work with root.

from random import choice
from tkinter import *


colors = ['blue','red','green', 'purple','dark red']
root = Tk()
root.geometry('300x300')

rows = 10
columns = 10

for i in range(rows):
    root.rowconfigure(index=i,weight=1)

for i in range(columns):
    root.columnconfigure(index=i, weight=1)

for r in range(rows):
    for c in range(columns):
        lbl  = Label(root,bg=choice(colors))
        lbl.grid(row=r,column=c,sticky='news')



if __name__ == '__main__':
    root.mainloop()

Example

  • Related