简体   繁体   English

按钮不会改变 tkinter 中的值

[英]Button won't change value in tkinter

I am attempting to make Tic Tac Toe using TKinter in Python.我正在尝试在 Python 中使用 TKinter 制作井字游戏。 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.我想制作一个 function ,它根据我在其目录中找到的__str__()属性在我的另一个二维数组中更改按钮值及其各自的 position。 Here is my function:这是我的 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.当我尝试按下前三个以外的按钮时,按钮上的值不会改变,但二维数组 position 会改变。 Have I done something wrong?我做错了什么吗? I can't seem to find it.我似乎找不到它。

Thanks in advance!提前致谢!

I found in the documentation a function called config() which you can apply.我在文档中找到了一个名为config()的 function,您可以应用它。 It worked for me!它对我有用!

For the buttons other than the first 3, you used == in:对于前 3 个以外的按钮,您使用==在:

self.cl['text'] == 'X' if self.p1turn else 'O'

It should be = instead:它应该是=

self.cl['text'] = 'X' if self.p1turn else 'O'

Note that using variable's internal name is not a good design.请注意,使用变量的内部名称不是一个好的设计。 Better use a 2D list (like self.board ) to store the buttons and use row and col to access them.最好使用 2D 列表(如self.board )来存储按钮并使用rowcol来访问它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM