I program a GUI in Python3 with Tkinter and place widgets with .grid()
. How can i tell Tkinter to place a new widget on the same row as the last placed widget but on a new, specified, column. The problem is that Tkinter automatically uses the next row when i don't specify the row and i don't want to hardcode the row number or pollute my code with a row counter that i have to increment every time i insert a widget in column 0.
What i tried:
#!/usr/bin/env python3
from tkinter import *
root=Tk()
Label(root, text="Label 0").grid(column=0)
Label(root, text="Label 1").grid(column=1)
Label(root, text="Label 2").grid(column=0)
Label(root, text="Label 3").grid(column=1)
Label(root, text="Label 4").grid(column=0)
Label(root, text="Label 5").grid(column=1)
root.mainloop()
How the layout looks like
Label 0
Label 1
Label 2
Label 3
Label 3
Label 5
How the layout should look like
Label 0 Label 1
Label 2 Label 3
Label 3 Label 5
What i don't want
It is possible to achieve the correct layout by specify the column number, like this:
#!/usr/bin/env python3
from tkinter import *
root=Tk()
Label(root, text="Label 0").grid(column=0,row=0)
Label(root, text="Label 1").grid(column=1,row=0)
Label(root, text="Label 2").grid(column=0,row=1)
Label(root, text="Label 3").grid(column=1,row=1)
Label(root, text="Label 4").grid(column=0,row=2)
Label(root, text="Label 5").grid(column=1,row=2)
root.mainloop()
But that is not flexible and inserting something in the second row means the code for all following rows have to be changed. Adding a row counter is possible, but this is error prone since it is easy to forget to increase the counter. Is there a way to insert a new widget on the second column on the same row as the last inserted widget without hardcoding or keep track of the last row number?
Or is there a function that returns the row of the last inserted widget? So that i can do this:
#!/usr/bin/env python3
from tkinter import *
root=Tk()
Label(root, text="Label 0").grid(column=0)
Label(root, text="Label 1").grid(column=1,row=<root.unknowFunctionToGetTheLastInsertedRow>)
Label(root, text="Label 2").grid(column=0)
Label(root, text="Label 3").grid(column=1,row=<root.unknowFunctionToGetTheLastInsertedRow>)
Label(root, text="Label 4").grid(column=0)
Label(root, text="Label 5").grid(column=1,row=<root.unknowFunctionToGetTheLastInsertedRow>)
root.mainloop()