Dova
Dova

Reputation: 310

Identify Widgets by Row/Column

Alright, so this question was sort of already answered here, but I looked through the code and couldn't seem to find where they actually called up the button by its Row/Column, as I am somewhat new to tkinter. This is the code I want:

for i in range(10):

    for j in range(10):

        if "" == #the button at the coordinates i,j 's text value :

            counter += 1

The problem is, I don't know the actual way of calling it up. Yes, I didn't identify them, as this is a variable Minesweeper application, so that's the best I can come up with.

Upvotes: 1

Views: 338

Answers (1)

Billal BEGUERADJ
Billal BEGUERADJ

Reputation: 22804

You are looking to identify a button based on its row and column location. This means you used grid() as the geometry manager and you draw the buttons on some parent widget I am going to call parent.

Based on your code and question's statement, you can use winfo_children() method to loop over the children/buttons of parent and grid_info() to access its children by row and column option values.

Here is identify_button_by_row_and_column() method that you can modify to fit your actual needs:

def identify_button_by_row_and_column(parent, row, column):
    for child in parent.winfo_children():
        info = child.grid_info()                            
        if info['row'] == str(row) and info['column'] == str(column):
            # Do or return something here

Upvotes: 1

Related Questions