AttributeError:'str'对象没有属性'disable' [英] AttributeError: 'str' object has no attribute 'disable'

查看:140
本文介绍了AttributeError:'str'对象没有属性'disable'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在学习guizero,并且我做出了一个在单击时禁用按钮的功能,因此很快遇到了一个问题.整个代码有点多,因此下面是适用于这种情况的代码.

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!

推荐答案

您的列表仅包含字符串,您无法对字符串执行.config(),请尝试

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

这篇关于AttributeError:'str'对象没有属性'disable'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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