【问题标题】:AttributeError: 'Button' object has no attribute 'set' - tkinterAttributeError:“按钮”对象没有属性“设置”-tkinter
【发布时间】:2012-01-02 02:35:04
【问题描述】:

我收到一个错误:

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

【问题讨论】:

    标签: tkinter attributeerror


    【解决方案1】:

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

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

    使用其中一个

    【讨论】:

      猜你喜欢
      • 2020-05-31
      • 2020-05-19
      • 2013-05-03
      • 2012-10-02
      • 2020-11-06
      • 2022-07-11
      • 1970-01-01
      • 2023-02-02
      • 1970-01-01
      相关资源
      最近更新 更多