Finding location of label in tkinter grid

Hi, I’m developing a project on Ludo game.
I’d like to know how to find the current grid point of a coin(Label).
So I can check like grid(coin)==grid(center) or not.

some_tk_obj.grid_info() may be what you are looking for.

1 Like

That’s a very good link from @sandraC and may very well be what you’re looking for.

I’m not sure that I understand your question, because with a .grid() layout, you are programmatically placing the ‘coin’ in a set position, so you ‘know’ where it is, therefore you don’t have to ‘find’ it.

This is a very simplistic demonstration:

# import tkinter module
from tkinter import *

# create the board
board = Tk()

# create the labels
l0 = Label(board, text = " 0 ")
l1 = Label(board, text = " 1 ")
l2 = Label(board, text = " 2 ")
l3 = Label(board, text = " 3 ")
l4 = Label(board, text = " 4 ")
l5 = Label(board, text = " 5 ")

# create the coin
coin = Label(board, text = "C")

# grid the labels
l0.grid(row = 0, column = 0)
l1.grid(row = 1, column = 0)
l2.grid(row = 2, column = 0)
l3.grid(row = 3, column = 0)
l4.grid(row = 4, column = 0)
l5.grid(row = 5, column = 0)

position = input("Where do you want the coin placed? ")

# grid the coin
coin.grid(row = position, column = 0)

print("The coin is in row",position)

mainloop()

Thank you Sandra !

No, I just needed to get the grid point of the coin and compare it with the grid point of center(win).
I don’t know where the coin will be as it is moving through a path according to no. cast on dice.
If they are equal, it should display winner :slight_smile:
That’s what I meant, sorry if my question was clumsy

No worries.

Just a thought: could you not keep a record of said path, so that you know where the coin is?

I’m sure your way is just as good, or even better.

All the best with it.

1 Like