Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8654901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:54:00+00:00 2026-06-12T14:54:00+00:00

Okay, the problem Im having is that I do not understand how to refer

  • 0

Okay, the problem Im having is that I do not understand how to refer to that particular button that was pressed. Im doing a game, if the use clicks a button I wanna know which X and Y coordinate it had. So, if you have a grid of buttons and you click one I want the coordinates and then I will change that button’s color. 🙂

PROBLEM: Knowing which button was pressed in a grid.

Thanks in advance!

def matris():
    for i in range(5):
            newButton = Button(app, width = 4, height = 2, bg = "blue",command = lambda i=i: function(i))
            newButton.grid(row = i, column = 0)

    for i in range(5):
            newButton = Button(app, width = 4, height = 2, bg = "blue",command = lambda i=i + 5: function(i))
            newButton.grid(row = i, column = 1)

    for i in range(5):
            newButton = Button(app, width = 4, height = 2, bg = "blue",command = lambda i=i + 10: function(i))
            newButton.grid(row = i, column = 2)

    for i in range(5):
            newButton = Button(app, width = 4, height = 2, bg = "blue",command = lambda i=i + 15: function(i))
            newButton.grid(row = i, column = 3)

    for i in range(5):
            newButton = Button(app, width = 4, height = 2, bg = "blue",command = lambda i=i + 20: function(i))
            newButton.grid(row = i, column = 4)


def function(i):
    if button 23 was clicked.changeColor to e.g "blue"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:54:01+00:00Added an answer on June 12, 2026 at 2:54 pm

    After reading all the comments and edits, it looks like what you really want to know is “which button was clicked?” when using the same command for multiple buttons.

    The easiest way to do that is to pass some sort of unique identifier to the command associated with the widget. The most straight-forward way is to pass a reference to the widget itself, though that requires a two step process.

    For example:

    this_button = Button(...)
    this_button.configure(command=lambda button=this_button: do_something(button))
    
    def do_something(button):
        print "you clicked this button:", button
    

    You can also use functools.partial to get the same result if you find lambda hard to wrap your head around:

    this_button.configure(command=functools.partial(do_something, this_button)
    

    If you prefer to create your button in a single step instead of two, you need some way to identify it. For me, the easiest way is with a dictionary. For example, if you’re creating rows and columns of buttons, you could do this:

    button = {}
    for r in range(10):
        for c in range(10):
            button[r,c] = Button(..., command=lambda row=r, column=c: do_something(row, ccolumn))
    
    def do_something(row, column):
        print "you clicked button", button[row,column]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I understand what the problem is, I sort of understand constraints in that
Okay, I'm not doing anything fancy here, but it's driving me crazy that I
Okay I'm having trouble not only with the problem itself but even with trying
Okay so I am having this problem. I write up a script to be
Okay, I seem to be having a problem. I'm trying to create a twicker
Okay guys, basically the problem I'm having is this. I've been assigned to write
Okay, so here's my problem: We use FOP for creating pretty report output. We
Okay so here is the problem I am having. I have several unit tests
I'm having a problem properly implementing generics in abstract classes that need to be
Okay so I am having some issues when doing some NSXMLParsing. The parsers are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.