繁体   English   中英

Tic Tac Toe Python GUI“方法”对象不可下标

[英]Tic Tac Toe Python GUI 'method' object is not subscriptable

我正在创建一个python tic tic tac toe游戏,而我目前遇到错误:

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等方法不是“可订阅的”对象,这意味着您不能

self._northwest[...]

因此python无法评估'if button ['image'] == ...'并输出该错误消息。

此外,根据您的代码,checker()希望将tkinter.Button实例作为参数,可以下标。 因此,您可能必须将其中一个按钮(self ._... Button)传递给该函数。

暂无
暂无

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

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