我没有得到任何错误,但代码没有做我想做的事情,所以我必须在代码中犯了一个错误。我想要做的是,如果单词匹配,那么这些单词必须是一对,并且两个选择的单元格应该保持“self.hidden = False”,因此单元格应该仍然显示两个单元格后面的单词。否则,如果单词不匹配,那么单元格应该是“self.hidden = True”,两个单元格应显示“---”。错误没有任何错误 - 包括Tkinter
这里是重要的部分:
from tkinter import *
import random
class Cell:
def __init__(self, word, hidden):
self.word = word
self.hidden = hidden
def show_word(self):
""" Shows the word behind the cell """
if self.hidden == True:
self.hidden = False
else:
self.hidden = True
self.button["text"] = str(self)
if mem.choice1 == None:
mem.choice1 = [self.word, self.hidden]
else:
mem.choice2 = [self.word, self.hidden]
self.check(mem.choice1, mem.choice2)
def check(self, choice1, choice2):
""" Checks if the chosen words are a pair """
tries = 0
if choice1 == choice2:
pass
else:
self.show_word
tries += 1
def __str__(self):
""" Displays or hides the word """
if self.hidden == True:
return "---"
else:
return self.word
class Memory(Frame):
""" GUI application that creates a Memory game """
def __init__(self, master):
super(Memory, self).__init__(master)
self.grid()
self.create_widgets()
self.tries = 0
self.choice1 = None
self.choice2 = None
def readShuffle(self):
""" Creates and organizes (shuffles) the pairs in a list """
# reads the file and creates a list of the words
words_file = open("memo.txt","r")
row = words_file.readline()
words = list()
while row != "":
row = row.rstrip('\n')
words.append(row)
row = words_file.readline()
words_file.close()
# shuffles the words in the list
random.shuffle(words)
# creates 18 pairs of words in a new list
the_pairs = list()
for i in range(18):
the_pairs.append(Cell(words[i],True))
the_pairs.append(Cell(words[i],True))
# shuffles the words in the new list
random.shuffle(the_pairs)
return the_pairs
def create_widgets(self):
""" Create widgets to display the Memory game """
# instruction text
Label(self,
text = "- The Memory Game -",
font = ("Helvetica", 12, "bold"),
).grid(row = 0, column = 0, columnspan = 7)
# buttons to show the words
column = 0
row = 1
the_pairs = self.readShuffle()
for index in range(36):
temp = Button(self,
text = the_pairs[index],
width = "7",
height = "2",
relief = GROOVE,
command = lambda x = index: Cell.show_word(the_pairs[x])
)
temp.grid(row = row, column = column, padx = 1, pady = 1)
column += 1
the_pairs[index].button = temp
if column == 6:
column = 0
row += 1
# total tries
self.label = Label(self)
Label(self,
text = "Total tries: 0",
font = ("Helvetica", 11, "italic")
).grid(row = 7, columnspan = 7, pady = 5)
# a quit button
Button(self,
text = "Quit",
font = ("Helvetica", 10, "bold"),
width = "25",
height = "1",
command = self.quit
).grid(row = 8, column = 0, columnspan = 7, pady = 5)
## def update_tries(self):
## """ Increase tries count and display new total. """
## self.tries += 1
## self.label["text"] = "Total Tries: " + str(self.tries)
def quit(self):
""" Ends the memory game """
global root
root.destroy()
# main
root = Tk()
root.title("Memory")
root.geometry("365x355")
mem = Memory(root)
root.mainloop()
我是新来的python,所以我不知道你的意思是什么“初始化”。我没有在这里做过? def __init __(self,word,hidden): self.word = word self.hidden = hidden – Amazon
如果你打算让人们为你调试你的代码,那么至少要让它成为一个独立的例子。 “重要位”忽略了许多其他重要的位。你正在做很多没有意义的事情(例如,'Cell.check'中的'try'将会被设置为'0')。你也有冗余的if/else语句。 (例如,你的第一个if/else在'Cell.show_word'等于'self.hidden = not self.hidden',整个'Cell.check'函数根本没有意义。) –
当然,我做了很多因为我是python的新手。我希望如果我在代码中某处出现了错误,那么我可以帮助我。 – Amazon