0

Im making a tic-tac-toe game and i need to check if the button text is 'X' or 'O' so i can check if there is a winner. Is there any way for me to do this? I tried this just as a random guess to see how it works:

if button1.config(text='X'):
    #x_won = True
else:
    pass

Is there any way for me to do this?

jjguidry
  • 43
  • 1
  • 1
  • 9

1 Answers1

0

You can access the buttons text by button1['text'] or any other of the buttons keys.

You can see the keys for the button by doing button1.keys()

You can check if button1['text'] == 'X': etc

Using button.config(text='X') is setting the buttons text to be X not checking for equality.

Pythonista
  • 11,377
  • 2
  • 31
  • 50
  • Is there a way to do what you did with the "if" statement without passing it an actual button name? – JumpingJacks May 22 '19 at 10:25
  • @Mike What you're asking doesn't make sense.... I accessed a class and an attribute of the class (in this case a component of a widget...). You can grab all widgets in a frame using `winfo_children` if I recall and iterate and check, but not all widgets have the same attributes. There are other roundabout ways... – Pythonista May 22 '19 at 11:17