AttributeError:“按钮"对象没有属性“设置"-tkinter [英] AttributeError: 'Button' object has no attribute 'set' - tkinter

查看:77
本文介绍了AttributeError:“按钮"对象没有属性“设置"-tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到错误:

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

我不知道要进行哪些更改才能使其正常工作.

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

这是代码的重要部分:

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

推荐答案

按钮没有 set 属性.
Tkinter窗口小部件属性设置有以下两种替代样式:

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

使用其中之一

这篇关于AttributeError:“按钮"对象没有属性“设置"-tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆