Tic Tac Toe Python GUI 'method' 对象不可订阅

Tic Tac Toe Python GUI 'method' object is not subscriptable

我正在创建一个 python 井字游戏,但我目前遇到错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1482, in __call__
    return self.func(*args)
  File "E:\Workspace\TTT3\src\ttt3.py", line 33, in <lambda>
    self._nwButton = Button(self, image = self.blankPhoto, command = lambda:checker(self._northwest))
  File "E:\Workspace\TTT3\src\ttt3.py", line 13, in checker
     if buttons["image"] == "self.blankPhoto" and xTurn == True:
TypeError: 'method' object is not subscriptable

我不确定它的确切含义,但这是我目前拥有的代码:

from tkinter import *
from PIL import Image, ImageTk
'''xTurn determines whos turn it is, game starts out with X's turn'''
xTurn = True

def checker(buttons):
    global xTurn
    if buttons["image"] == "self.blankPhoto" and xTurn == True:
        print("X's turn")
        xTurn = False
    elif buttons["image"] == "self.blankPhoto" and xTurn == False:
        print("O's turn")
        xTurn = True


class tttGUI(Frame):
    def __init__(self):

       '''Setup GUI'''
       Frame.__init__(self)
       self.master.title("Tic-Tac-Toe GUI")
       self.grid()

       self.buttons = StringVar()

       self.blankPhoto = PhotoImage(file = "blank.gif")

       self._nwButton = Button(self, image = self.blankPhoto, command = lambda:checker(self._northwest))
       self._nwButton.grid(row = 0, column = 0)

       self._nButton = Button(self, image = self.blankPhoto, command = lambda:checker(self._north))
       self._nButton.grid(row = 0, column = 1)

       self._neButton = Button(self, image = self.blankPhoto, command = lambda:checker(self._northeast))
       self._neButton.grid(row = 0, column = 2)

       self._wButton = Button(self, image = self.blankPhoto, command = lambda:checker(self._west))
       self._wButton.grid(row = 1, column = 0)

       self._cButton = Button(self, image = self.blankPhoto, command = lambda:checker(self._center))
       self._cButton.grid(row = 1, column = 1)

       self._eButton = Button(self, image = self.blankPhoto, command = lambda:checker(self._east))
       self._eButton.grid(row = 1, column = 2)

       self._swButton = Button(self, image = self.blankPhoto, command = lambda:checker(self._southwest))
       self._swButton.grid(row = 2, column = 0)

       self._sButton = Button(self, image = self.blankPhoto, command = lambda:checker(self._south))
       self._sButton.grid(row = 2, column = 1)

       self._seButton = Button(self, image = self.blankPhoto, command = lambda:checker(self._southeast))
       self._seButton.grid(row = 2, column = 2)

    '''Buttons'''
    def _northwest(self):
        print("North-West")


    def _north(self):
        print("North")

    def _northeast(self):
        print("North-East")

    def _west(self):
        print("West")

    def _center(self):
        print("Center")

   def _east(self):
       print("East")

   def _southwest(self):
       print("South-West")

   def _south(self):
       print("South")

   def _southeast(self):
       print("South-East")

def main():
    tttGUI().mainloop()

main()

我正在尝试弹出一个 GUI,只要您单击其中一个按钮,它就会根据轮到谁而变为 X 或 O。

正如您的 python 所说,您正在将 方法 self._something 传递给函数 checker()。

Button( ..., command = lambda: checker(self._northwest))

并且方法 self._northwest、self._north、... 不是 'subscriptable' 对象,这意味着您不能

self._northwest[...]

因此 python 无法评估“if buttons['image'] == ...”并打印该错误消息。

此外,根据您的代码,checker() 需要一个 tkinter.Button 的实例作为参数,它是可订阅的。因此,您可能必须将其中一个按钮 (self._...Button) 传递给函数。