Variable for referring to Buttons

So, I want to make a TicTacToe game using tkinter. I dont want to write a function for each Button but one function every Button uses.

from tkinter import *

class TicTacToe:

def __init__(self, master):
    frame = Frame(master)

    self.knopf1 = Button(master, width=7, height=3, command=lambda: self.Kreuz(1))
    self.knopf1.grid(column=1, row=1, padx=3, pady=3)

    self.knopf2 = Button(master, width=7, height=3, command=lambda: self.Kreuz(2))
    self.knopf2.grid(column=2, row=1, padx=3, pady=3)

def Kreuz(self,c):
    self.(here I want the Button that was pressed).config(text='X')

root = Tk()

Test = TicTacToe(root)

root.resizable(width=False, height=False)

Now my question is, how I can make the method know which Button was pressed.

The same way that you passed c! Just pass self.knopf1 or self.knopf2 as parameters.

1 Like