我正在创建一个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取决于谁是轮到它。
发布于 2016-05-09 21:37:01
正如您的python所述,您正在将方法 self._something传递给函数检查器()。
Button( ..., command = lambda: checker(self._northwest))以及self._northwest,self._north,.不是“可订阅”对象,这意味着您不能
self._northwest[...]因此python无法计算“if按钮”图像“== .”打印错误信息。
此外,根据您的代码,checker()需要一个实例tkinter.Button作为参数,该参数是可订阅的。因此,您可能必须将一个按钮(self._...Button)传递给函数。
https://stackoverflow.com/questions/37125631
复制相似问题