我为一个介绍python类的最终项目创建了一个Hangman类和一个HangmanGUI类。如果我犯了业余错误,我很抱歉,但我没有很强的编码背景。每次我的合作伙伴和我运行GUI类时,都会收到此错误:
self.__letterVar.set(self.__ans.getAnswer())
TypeError: set() missing 1 required positional argument: 'value'
这是刽子手类:
from random import *
FOUR = 4
class Hangman:
# --------------------------------------------------------------------------
# Constructor
def __init__(self):
self.__theWord = ''
self.__indexList = []
self.__answer = '_ _ _ _ _ _'
# ---------------------------------------------------------------------------
# Accessors
def getTheWord(self):
return self.__theWord
def getIndexList(self):
return self.__indexList
def getAnswer(self):
return self.__answer
def startFruit(self):
randNumber = randint(0,FOUR)
fruit = open("fruits.txt", 'r')
fruitList = fruit.read().splitlines()
self.__theWord = fruitList[randNumber]
return self.__theWord
def startVege(self):
randNumber = randint(0,FOUR)
vege = open("veges.txt", 'r')
vegeList = vege.read().splitlines()
self.__theWord = vegeList[randNumber]
return self.__theWord
def startName(self):
randNumber = randint(0,FOUR)
name = open("names.txt", 'r')
nameList = name.read().splitlines()
self.__theWord = nameList[randNumber]
return self.__theWord
# --------------------------------------------------------------------------
# Mutators
def find(self, answerWord, inputLetter):
if inputLetter in answerWord:
self.__indexList = [index for index, letter in enumerate(answerWord) if letter == inputLetter]
else:
self.__indexList = []
def set (self, inputLetter):
newString = ""
textList = list(self.__answer)
self.__indexList = self.getIndexList()
location = self.__indexList
for aNum in location:
textList[aNum * 2] = inputLetter
newString = ''.join(textList)
self.__answer = newString
def reset(self):
self.__answer = '_ _ _ _ _ _'
# --------------------------------------------------------------------------
# toString()
def__str__(self):
return "Current answer is: %s" % self.__answer
'''
def main():
game = Hangman()
keyWord = game.startFruit()
letter = input("enter letter")
while letter:
game.find(keyWord, letter)
game.set(letter)
print(game)
letter = input("enter letter")
main()
'''
在同一个文件夹中有几个名为Fruits,Veges和Names的文件,每个文件在各自的类别中有大约5-6个六个字母的单词。我想让程序打开并读取文件并在列表中随机提取一个单词。
我们测试了我们的hangman类,我们认为它工作正常,但我认为我们的问题源于我们的HangmanGUI类。
from tkinter import *
from random import *
from hangman import *
FOUR = 4
class HangmanUI(Frame):
def __init__(self):
Frame.__init__(self)
self.master.title("Hangman")
self.grid()
self.__label = Label(self, text = "Let's play Hangman!")
self.__label2 = Label(self, text = "Please choose a category!")
self.__label2.grid(row = 1, column = 0)
self.__buttonPane = Frame(self)
self.__buttonPane.grid(row = 2, column = 0)
# created a model of the hangman class
self.__ans = Hangman()
self.__fruitButton= Button(self.__buttonPane, text = 'Fruits', command = self.startFruit)
self.__nameButton = Button(self.__buttonPane, text = 'Names' , command = self.startName)
self.__vegeButton = Button(self.__buttonPane, text = 'Veges', command = self.startVege)
self.__fruitButton.grid(row = 2, column = 0)
self.__vegeButton.grid(row = 2, column = 1)
self.__nameButton.grid(row = 2, column = 2)
self.__label.grid(row = 0 , column = 0)
#to change later so if shuffles images 1-10 with each error (for loop)
self.__image = PhotoImage(file = "0.gif")
self.__imageLabel = Label(self, image = self.__image)
self.__imageLabel.grid(row = 3, column = 0)
self.__letterLabel = Label(self, text = "Please submit a letter to play!")
# instead of the submit button, tried using enter instead for now
self.__letterVar= StringVar
#self.__letterEntry = Entry(self, textvariable = self.__letterVar) # commented out this line, maybe i shouldn't have?
self.__letterEntry = Entry(self, width = 10)
self.__letterEntry.bind('<Return>', self.set)
self.__letterLabel.grid(row = 4, column = 0)
self.__letterEntry.grid(row = 5, column = 0)
self.__value = Label(self, textvariable = self.__letterVar)
self.__value.grid(row= 6, column = 0)
#creates nested frame
self.__letterPane = Frame(self)
self.__letterPane.grid(row = 7, column = 0)
self.__letter1 = Label(self.__letterPane, text = '_ _ _ _ _ _')
self.__letter1.grid(row = 7, column = 0)
def startFruit(self):
self.__ans.startFruit()
def startVege(self):
self.__ans.startVege()
def startName(self):
self.__ans.startName()
def set(self,event):
guessWord = self.__ans.getTheWord()
entryLetter = self.__letterEntry.get()
self.__ans.find(guessWord, entryLetter)
newValue = self.__letterEntry.get()
self.__ans.set(newValue)
# display new value in letterVar
# this is where the program crashes
self.__letterVar.set(self.__ans.getAnswer())
# invoke delete() to clear entry box
self.__letterEntry.delete(0,END)
def main():
HangmanUI().mainloop()
main()
对于长码,很抱歉,这是我第一次在这里发帖,我们都非常紧张,因为我们大学的决赛周,我们一直疯狂地试图把它放在一起。我们都不是复合科学大学,所以这是一个相当困难的过程。
我们不确定如何调用Hangman类并使用GUI中Hangman类的方法。截至目前,我们已收到上述错误,而且我们不确定如何修复错误。程序在def set(self,event)的GUI类结束时崩溃:
如果有人能提供任何见解和提示,那将非常感谢!
答案 0 :(得分:2)
您需要__letterVar
StringVar
{em}
self.__letterVar = StringVar()
否则其上的set()
方法未绑定,并且不会获得自动self
第一个参数。
您的代码将其指定为类,而不是新实例。