问题
I am currently learning guizero and I ran into an issue fairly quickly with making a function that disables buttons when clicked. The entire code is a bit much so below is the code that's applicable to the situation.
buttons = ["button0", "button1"]
def disable(n):
buttons[n].disable()
menu = Box(app, layout="grid", grid=[1,0])
button0 = PushButton(menu, command=lambda: disable(0), text="x", grid=[0,0])
button1 = PushButton(menu, command=lambda: disable(1), text="x", grid=[1,0])
Unfortunately, this code returns the following error that is painfully hard to figure out
Exception in Tkinter callback
Traceback (most recent call last):
File "/Users/oliver/.conda/envs/py37/lib/python3.7/tkinter/__init__.py", line 1702, in __call__
return self.func(*args)
File "/Users/oliver/.conda/envs/py37/lib/python3.7/site-packages/guizero/PushButton.py", line 197, in _command_callback
self._command()
File "guizero_test.py", line 84, in <lambda>
button0 = PushButton(menu, command=lambda: disable(0), text="0", grid=[0,0])
File "guizero_text.py", line 14, in disable
buttons[n].disable()
AttributeError: 'str' object has no attribute 'disable'
Any help with figuring this out is appreciated!
回答1:
def disable(n):
buttons[n].disable()
disable() this is the method it calling itself method disable() if assign disable property like that button[n].config(state="disabled")
replace with line 14
button[n].config(state="disabled")
来源:https://stackoverflow.com/questions/59341156/attributeerror-str-object-has-no-attribute-disable