Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

python - Button won't change value in tkinter

I am attempting to make Tic Tac Toe using TKinter in Python. I wanted to make a function where it changes the button value and its respective position in my other 2D array based on its __str__() attribute that I found in its directory. Here is my function:

    def changed_based_on_id(self,btn):
        if btn.__str__() == '.!button' and btn['text'] == '':
            self.ld['text'] = 'X' if self.p1turn else 'O'
            self.board[0][0] = 'X' if self.p1turn else 'O'
            self.turn()
        elif btn.__str__() == '.!button2' and btn['text'] == '': 
            self.tc['text'] = 'X' if self.p1turn else 'O'
            self.board[0][1] = 'X' if self.p1turn else 'O'
            self.turn()
        elif btn.__str__() == '.!button3' and btn['text'] == '': 
            self.rd['text'] = 'X' if self.p1turn else 'O'
            self.board[0][2] = 'X' if self.p1turn else 'O'
            self.turn()
        elif btn.__str__() == '.!button4' and btn['text'] == '':
            self.cl['text'] == 'X' if self.p1turn else 'O'
            self.board[1][0] = 'X' if self.p1turn else 'O'
            self.turn()
        elif btn.__str__() == '.!button5' and btn['text'] == '':
            self.c['text'] == 'X' if self.p1turn else 'O'
            self.board[1][1] = 'X' if self.p1turn else 'O'
            self.turn()
        elif btn.__str__() == '.!button6' and btn['text'] == '':
            self.cr['text'] == 'X' if self.p1turn else 'O'
            self.board[1][2] = 'X' if self.p1turn else 'O'
            self.turn()
        elif btn.__str__() == '.!button7' and btn['text'] == '':
            self.bld['text'] == 'X' if self.p1turn else 'O'
            self.board[2][0] = 'X' if self.p1turn else 'O'
            self.turn()
        elif btn.__str__() == '.!button8' and btn['text'] == '':
            self.bc['text'] == 'X' if self.p1turn else 'O'
            self.board[2][1] = 'X' if self.p1turn else 'O'
            self.turn()
        elif btn.__str__() == '.!button9' and btn['text'] == '':
            self.brd['text'] == 'X' if self.p1turn else 'O'
            self.board[2][2] = 'X' if self.p1turn else 'O'
            self.turn()
            print(self.board)
        else:
            pass

When I try to press a button other than the first three, the value on the button does not change, but the 2D array position does. Have I done something wrong? I can't seem to find it.

Thanks in advance!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I found in the documentation a function called config() which you can apply. It worked for me!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...