AttributeError: 'Button' object has no attribute 'set' - tkinter

流过昼夜 提交于 2021-01-27 19:04:34

问题


I'm getting an error:

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

I can't figure out what to change to make it work.

Here is the important parts of the code:

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

回答1:


Button's have no set attribute.
Tkinter widget properties are set with these two alternative styles:

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

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

Use one of them



来源:https://stackoverflow.com/questions/8197509/attributeerror-button-object-has-no-attribute-set-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!