tkinter - AttributeError: 'Button' 对象没有属性 'set' - tkinter

标签 tkinter attributeerror

我收到错误:

self.button.set(str(self))
AttributeError: 'Button' object has no attribute 'set'

我不知道要更改什么才能使其正常工作。

以下是代码的重要部分:

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 = 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

最佳答案

Button 没有 set 属性。
Tkinter 小部件属性使用以下两种替代样式设置:

self.button["text"] = str(self)

self.button.config(text=str(self))

使用其中之一

关于tkinter - AttributeError: 'Button' 对象没有属性 'set' - tkinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8197509/

相关文章:

python - AttributeError: 模块 'datetime' 没有属性 'time'

Python tkinter slider 控件

python - 如何使用tix.DirSelectDialog?

Python MVC 风格的 GUI 温度转换器

python - 为什么正则表达式函数总是弹出属性错误?

python - AttributeError: 模块 'os' 没有属性 'chroot'

python - 打印点击按钮的文本 tkinter

python - 如何获取组合框中所选项目的索引?

keras - 模块 'keras.optimizers' 没有属性 'SGD' 。谷歌实验室

Python *args 属性错误