0

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!

2 Answers2

0
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")
Mr Coder
  • 507
  • 3
  • 13
  • Thank you for your response! I am now getting `AttributeError: 'str' object has no attribute 'config'` when the button is pressed. – Oliver Walsh Dec 15 '19 at 15:03
0

your list just contains strings and you can't perform .config() on a string, try

buttons = [PushButton(menu, command=lambda: disable(0), text="x", grid=[0,0])]
buttons += (PushButton(menu, command=lambda: disable(1), text="x", grid=[1,0]))

when creating your buttons so the list contains the actual button objects

Ethan2001
  • 51
  • 4