我在标签小部件的文本周围得到了大括号.输出{Total tries: 0}代替Total tries: 0.
这是我的代码的简短版本:
class Cell:
def check(self):
mem.tries += 1
mem.update_tries()
class Memory(Frame):
def __init__(self, master):
super(Memory, self).__init__(master)
self.grid()
self.create_widgets()
self.tries = 0
def create_widgets(self):
self.label = Label(self)
self.label["text"] = "Total tries: 0",
self.label["font"] = ("Helvetica", 11, "italic")
self.label.grid(row = 7, columnspan = 7, pady = 5)
def update_tries(self):
self.label["text"] = "Total tries: " + str(self.tries)
root = Tk()
root.title("Memory")
root.geometry("365x355")
mem = Memory(root)
root.mainloop()
Run Code Online (Sandbox Code Playgroud) 我收到一个错误:
self.button.set(str(self))
AttributeError: 'Button' object has no attribute 'set'
Run Code Online (Sandbox Code Playgroud)
我不知道要进行哪些更改才能使其正常工作。
这是代码的重要部分:
class Cell:
def show_word(self):
""" Shows the word behind the cell """
if self.hidden == True:
self.hidden = False
else:
self.hidden = True
self.button.set(str(self))
class Memory(Frame):
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 …Run Code Online (Sandbox Code Playgroud)