次のエラーが表示されます。
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1470, in __call__
return self.func(*args)
TypeError: <lambda>() takes exactly 1 argument (0 given)
このクラスを呼び出すと
class settingsWindow:
def __init__(self, master, settings):
self.master = master
self.titleLb = tk.Label(self.master, text='MesterMind', font=('Calibri', 22))
self.titleLb.grid(row=0, column=0, sticky='W', columnspan=2)
self.subTitleLb = tk.Label(self.master, text="Settings", font=('Calibri', 18))
self.subTitleLb.grid(row=1, column=0, sticky='W', columnspan=2)
これを使用してクラスのインスタンスを作成します
def settings(self):
self.settingsWindow = tk.Toplevel(self.master)
self.app = settingsWindow(self.settingsWindow, self.settings)
エラーの原因がわかりません。コードに明らかなエラーは見られません。私はpython 2.7を使用しています
どんな助けでも大歓迎です、ありがとう。
編集:NB、tkはTkinterモジュールです
ここで編集するのは、python モジュール全体 (MasterMind.py) です。関連する部分はすべて上にあると思います。
'''
Created on 9 Nov 2013
@author: SB960
'''
import Tkinter as tk
def settingsArgs():
'''
stores the settings for the game.
no param = get value
param = assign value | returns true if value assigned, false otherwise.
everything stored here is a constant for the duration of the game
'''
prefDic = {'COLOURS': 3,
'PEGS': 3,
'ROWS': 3,
'PLAYER1': 'COMPUTER',
'PLAYER2': 'COMPUTER'} #Dictionary containing preference values
def colours(int1 = 0):
if int1 == 0:
return prefDic['COLOURS']
elif 3 <= int1 <= 8:
prefDic['COLOURS'] = int1
return True
else:
return False
def pegs(int1 = 0):
if int1 == 0:
return prefDic['PEGS']
elif 3 <= int1 <= 8:
prefDic['PEGS'] = int1
return True
else:
return False
def rows(int1 = 0):
if int1 == 0:
return prefDic['ROWS']
elif 3 <= int1 <= 12:
prefDic['ROWS'] = int1
return True
else:
return False
def player1(string1 = ''):
if string1.isalnum():
prefDic['PLAYER1'] = string1
return True
elif string1 == '':
return prefDic['PLAYER1']
else:
return False
def player2(string1 = ''):
if string1.isalnum():
prefDic['PLAYER2'] = string1
return True
elif string1 == '':
return prefDic['PLAYER2']
else:
return False
def copy(settings):
colours(settings('colours')())
pegs(settings('pegs')())
rows(settings('rows')())
player1(settings('player1')())
player2(settings('player2')())
def importFromFile(fileName = 'SETTINGS'):
'''
imports saved settings from fileName. see exprtTofile
'''
try:
f = open(fileName, 'r')
except:
return False
header = f.readline().strip()
if header == 'MASTERMINDSETTINGS':
settingsStr = f.readline().strip()
settingsLs = settingsStr.split('/')
for i in range(0, len(settingsLs), 2):
prefDic[settingsLs[i]] = int(settingsLs[i+1])
settingsStr = f.readline().strip()
settingsLs = settingsStr.split('/')
for i in range(0, len(settingsLs), 2):
prefDic[settingsLs[i]] = str(settingsLs[i+1])
f.close()
def exportToFile(fileName = 'SETTINGS'):
'''
exports current settings to file
stored of form:
MASTERMINDSETTINGS
attribute1/decimal1/attribute2/decimal2/...
attribute1/string1/...
'''
f = open(fileName, 'w')
f.write('MASTERMINDSETTINGS')
f.write('COLOURS', prefDic['COLOURS'], 'PEGS', prefDic['PEGS'], 'ROWS', prefDic['ROWS'], sep = '/' )
f.write('PLAYER1', prefDic['PLAYER1'], 'PLAYER2', prefDic['PLAYER2'])
f.close()
return (lambda op: {'COLOURS': colours,
'PEGS': pegs,
'ROWS': rows,
'PLAYER1': player1,
'PLAYER2': player2,
'COPY': copy,
'IMPORT': importFromFile}[op.upper()])
def boardArgs():
'''
Stores ongoing data about the current game
'''
data = {'ROW': 0,
'POINTS': 0,#the points of player1; see points function
'CODE': [], #colours stored as a list of ints, 0 = no colour, 1 <= x <= colours is id for colour.
'BOARD': [], #^ditto except nested list for each row
'FEEDBACK': [] #^ditto except 0 <= x <= 2
}
#get data functions:
def currentRow():
return data['ROW']
def points(player = 1):#player = 1 or 2
if player == 1:
return data['POINTS']
elif player == 2:
return ((data['ROUND'] - 1) - data['POINTS'])
else:
return 0
def code():
return data['CODE']
def board(row, peg):
row -= 1 #as in list i starts at 0
peg -= 1 #^ditto
try:
return data['BOARD'][row][peg]
except:
return 0
def feedback(row, peg):
row -= 1 #as in list i starts at 0
peg -= 1 #^ditto
try:
return data['FEEDBACK'][row][peg]
except:
return 0
#assign data functions:
def newGame(settings): #sets dimensions of boards and initial values
data['CODE'] = [0] * settings('PEGS')()
data['BOARD'] = [data['CODE']] * settings('rows')()
data['FEEDBACK'] = [data['CODE']] * (1 + settings('rows')())
data['ROW'] = 0
data['POINTS'] = 0
def makeMove(code1Ls, settings):
code2Ls = data['CODE'][:]#creates duplicate of code
feedback = []#creates feedback list
del feedback[0]#empties feedback list
#compare for colour AND position: remove when found
for i in range(len(code2Ls)):
if code1Ls[i] == code2Ls[i]:
feedback.append(2)
del code1Ls[i]
del code2Ls[i]
#compare for colour: remove from code2 only as element of code1 will not be checked thereafter
for x in code1Ls:
for i in range(len(code2Ls)):
if x == code2Ls[i]:
feedback.append(1)
del code2Ls[i]
#ensure feedback is congruent to elements of data[feedback]:
while len(feedback) < settings('PEGS'): feedback.append(0)
#assign values:
data['FEEDBACK'][data['ROW']] = feedback
data['ROW'] += 1
return (lambda x: {'ROW': currentRow,
'POINTS': points,
'CODE': code,
'BOARD': board,
'FEEDBACK': feedback,
'NEWGAME': newGame,
'MAKEMOVE': makeMove}[x.upper()])
def main():
root = tk.Tk()
app = mainWindow(root)
root.mainloop()
class mainWindow:
def __init__(self, master):
self.settings = settingsArgs()
self.settings('import')()
self.master = master
self.frame = tk.Frame(self.master)
self.master.geometry('+400+200')
self.master.title('MasterMind')
self.title = tk.Label(self.master, text = 'MasterMind', font = ('Calibri', 22))
self.title.pack()
self.newGameBtn = tk.Button(self.frame, text = 'New Game', width = 25, command = self.newGame)
self.newGameBtn.pack()
self.loadGameBtn = tk.Button(self.frame, text = 'Load Game', width = 25, command = self.loadGame)
self.loadGameBtn.pack()
self.settingsBtn = tk.Button(self.frame, text = 'Settings', width = 25, command = self.settings)
self.settingsBtn.pack()
self.frame.pack()
def newGame(self):
self.gameWindow = tk.Toplevel(self.master)
self.app = gameWindow(self.gameWindow, self.settings)
def loadGame(self):
pass
def settings(self):
self.settingsWindow = tk.Toplevel(self.master)
self.app = settingsWindow(self.settingsWindow, self.settings)
class gameWindow:
def __init__(self, master, settings):
self.master = master
self.settings = settingsArgs()
self.settings('copy')(settings)
self.frame = tk.Frame(self.master)
self.quitButton = tk.Button(self.frame, text = 'quit', width = 25, command = self.close_windows)
self.quitButton.pack()
self.frame.pack()
def close_windows(self):
self.master.destroy()
class settingsWindow:
def __init__(self, master, settings):
self.master = master
self.titleLb = tk.Label(self.master, text='MesterMind', font=('Calibri', 22))
self.titleLb.grid(row=0, column=0, sticky='W', columnspan=2)
self.subTitleLb = tk.Label(self.master, text="Settings", font=('Calibri', 18))
self.subTitleLb.grid(row=1, column=0, sticky='W', columnspan=2)
main()