Python 如何更改self.button的状态

Python 如何更改self.button的状态,python,python-3.x,oop,tkinter,Python,Python 3.x,Oop,Tkinter,我试着用Tkinter学习面向对象。 我制作了一个Button类并尝试使用它,但我无法更改它的状态 这是我的课 class Buton(): def __init__(self, konum=None, text=None, command=None, cursor="hand2", state="normal", width=None, row=None, column=None, sticky=None): print("SELF:

我试着用Tkinter学习面向对象。 我制作了一个Button类并尝试使用它,但我无法更改它的状态

这是我的课

class Buton():
    def __init__(self, konum=None, text=None, command=None, cursor="hand2", state="normal", width=None,
                 row=None, column=None, sticky=None):
        print("SELF: ", type(self), self)
        self.konum = konum
        self.text = text
        self.command = command
        self.cursor = cursor
        self.state = state
        self.width = width
        self.row = row
        self.column = column
        self.sticky = sticky
        self.buton_bas()

    def buton_bas(self):
        self.buton = Button(self.konum, text=self.text, state=self.state, command=self.command)
        self.buton.grid(row=self.row, column=self.column, padx=2, pady=5)

    def state_change(self, new_state):
        self.['state'] = new_statew
我的def和按钮

def print():
    global label
    label = Label(pencere, text=entry.get(), fg="lightgray", bg=darkbg, font=("Times 10"))
    label.grid(row=3, column=0, columnspan=2, padx=2, pady=5, )
    Buton.state_change(buton1, "disable")

buton1 = Buton(konum=pencere, text="Print", command=print, row=2, column=0)
当我按下打印按钮时,它会打印输入的文本。没关系,但我不能改变按钮的状态。我认为:TypeError:“Buton”对象不支持项分配

我尝试

self.state = new_state
但是不要工作。。怎么了?
感谢您从现在起提供的帮助

您的代码包含许多错误。我已经修好了。您需要使用
[“state”]=state
。尝试:

from tkinter import *
root = Tk()
class Buton():
    def __init__(self, konum=None, text=None, command=None, cursor="hand2", state="normal", width=None,
                 row=None, column=None, sticky=None):
        #print("SELF: ", type(self), self)
        self.konum = konum
        self.text = text
        self.command = command
        self.cursor = cursor
        self.state = state
        self.width = width
        self.row = row
        self.column = column
        self.sticky = sticky
        self.buton_bas()

    def buton_bas(self):
        self.buton = Button(self.konum, text=self.text, state=self.state, command=self.command)
        self.buton.grid(row=self.row, column=self.column, padx=2, pady=5)

    def state_change(self, new_state):
        self.buton['state'] = new_state
def print():
    global label
    label = Label(root, text="You can change text", fg="lightgray", bg="grey", font=("Times 10"))
    label.grid(row=3, column=0, columnspan=2, padx=2, pady=5, )
    Buton.state_change(buton1, "disable")

buton1 = Buton(konum=root, text="Print", command=print, row=2, column=0)    

我想我在尝试只传输一些代码时犯了一些错误。但是当你解决了这个问题,问题就解决了。非常感谢您有一个名为print的函数,它覆盖了内置的方法
print
。您应该重命名该函数。