如何更改python(3)中列表内所有对象的属性? [英] How to change the attributes for all objects inside a list in python(3)?

查看:61
本文介绍了如何更改python(3)中列表内所有对象的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,不熟悉操作 Python 列表.我有一个 tkinter 小部件列表,更具体地说是按钮,它们已被添加到我的列表 btnList 中:

I am new to python and unfamiliar with manipulating python lists. I have a list of tkinter widgets, more specifically buttons, which have been added to my list btnList by using:

btnList.append(btn1)

btnList.append(btn2)

通常要更改对象的属性值(例如状态属性),可以使用:

Normally to change an object's attribute value (the state attribute for example) one would use:

btn1.configure(state='disabled')

btn2.configure(state='disabled')

这会将两个按钮的状态属性设置为禁用,

which would set the state attribute of both buttons to disabled,

有没有办法更改列表中包含的所有对象的属性?例如将每个按钮的状态设置为禁用?

Is there a way to change attributes for all of the objects contained in the list? for example setting the state of each button to disabled?

推荐答案

只需循环遍历您的列表:

Simply loop over your list:

for button in btnList:
    button.configure(state='disabled')

button 将从您的列表中依次分配每个按钮,让您在其上调用 configure() 方法.

button will be assigned each button from your list in turn letting you call the configure() method on it.

这篇关于如何更改python(3)中列表内所有对象的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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