简体   繁体   English

从循环更改多个屏幕的kivy小部件属性

[英]Changing kivy widget properties for multiple screens from a loop

I have a bunch of screens in my app that all have the same icon that I always want to change together. 我的应用程序中有一堆屏幕,所有的屏幕都有相同的图标,我总是想一起更改。 So I currently have it hard coded and everytime I add a new screen I have to add a new line and it's getting cumbersome: 因此,目前我已经对其进行了硬编码,并且每次添加新屏幕时都必须添加新行,并且变得越来越麻烦:

self.tcs_screen.ids.statusicon.source = "/imgs/..."
self.eclss_screen.ids.statusicon.source = "/imgs/..."
self.gnc_screen.ids.statusicon.source = "/imgs/..."
...

Is it possible to do this from a loop over a list of the screens? 是否可以通过循环浏览屏幕列表来执行此操作? I've been trying the following with no success (how do you insert a variable into a property assignment?): 我一直在尝试以下操作而没有成功(如何将变量插入属性分配?):

ScreenList = ['tcs_screen', 'eclss_screen', 'gnc_screen']
for x in xrange(len(ScreenList)):
        self.ScreenList[x].ids.statusicon.source = "/imgs/..."

Or is there a better way to accomplish this? 还是有更好的方法来做到这一点?

Thanks 谢谢

You have to use getattr() to get the property using the name. 您必须使用getattr()来使用名称获取属性。

screen_list = ['tcs_screen', 'eclss_screen', 'gnc_screen']

for e in screen_list:
    getattr(self, e).ids.statusicon.source = "/imgs/..."

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM