【发布时间】:2018-08-22 17:07:25
【问题描述】:
我尝试使用 Tkinter 学习 Object Orientet。 我制作了一个按钮类并尝试使用它,但我无法更改它的状态
这是我的课
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: 'Button' object does not support item assignment
我试试;
self.state = new_state
但不工作..有什么问题? 从现在开始感谢您的帮助
【问题讨论】:
-
您有一个名为 print 的函数,它覆盖了内置方法
print。您应该重命名该函数。
标签: python python-3.x oop tkinter