Peter Moisan
Peter Moisan

Reputation: 1

Using an argument to change the config on a button?

I am working on a battleship game, (Yes it can be simplified a lot, i am novice to code but ahead in my high school class).

from tkinter import *
window = Tk()

mainFrame = Frame(window, width=500, height=500)
mainFrame.grid(row=0, column=0)

listShot = []

def shootAt(location):
    print(location)
    if(location in listShot):
        print()
    else:
        listShot.append(location)
        print(listShot)
        location.config(relief = SUNKEN)

A1 = Button(mainFrame, text="X", width = 4, height = 2, 
            command = lambda: shootAt(A1))
A1.grid(row=1, column=1, padx=2, pady=2)

How would I use an argument in my buttons to change the config of the called button?

Upvotes: 0

Views: 32

Answers (1)

Tom Fuller
Tom Fuller

Reputation: 5349

You can use lambda to give as many arguments as you want to the shootAt() function

Please note: fg is just an example, you can change this to whatever you want

def shootAt(location, CONFIGURE_THIS):
    ...
    else:
        ...
        location.config(relief = SUNKEN, fg = CONFIGURE_THIS)

A1 = Button(mainFrame, text="X", width = 4, height = 2, 
            command = lambda: shootAt(A1, "orange"))

Upvotes: 1

Related Questions