0
votes

Is there a way to get the last row and column used in the root window or a toplevel using tkinter ?

The purpose is to add entry boxes two by two on a new line each time a button is pressed.

So i would use the information of the last used row to position the new generated entry boxes with grid().

Thanks in advance.

3
Just use a global variable that stores the last used grid row. - TheLizzard

3 Answers

1
votes

You can use the grid_size method, which returns the highest numbered row and column.

This is what the official documentation says:

Returns the size of the grid (in columns then rows) for container. The size is determined either by the content occupying the largest row or column, or the largest column or row with a -minsize, -weight, or -pad that is non-zero.

0
votes

You can keep track of grid by using grid_info()

Here is a code snippet that demonstrates this.

import tkinter as tk

root = tk.Tk()

e = tk.Entry( root )
e.grid( row = 0, column = 0, sticky = tk.NSEW )
print( e.grid_info() )
0
votes

Thanks to Bryan Oakley's solution :

from tkinter import *

#------------------------------------

def addBox():

    row=root.grid_size()
    row = row[1]
    ent = Entry(root)
    ent.grid(row = row,column=0)
    ent1 = Entry(root)
    ent1.grid(row = row, column=1)

    all_entries.append(ent)
    all_entries.append(ent1)


#------------------------------------

def showEntries():

    for number, ent in enumerate(all_entries):
        print (number, ent.get())

#------------------------------------

all_entries = []

root = Tk()

showButton = Button(root, text='Print Text of each Entry Box', command=showEntries)
showButton.grid(row=0, column = 0)

addboxButton = Button(root, text='Add Entry Boxes', fg="Red", command=addBox)
addboxButton.grid(row=0, column = 1)

root.mainloop()