简体   繁体   English

在循环中动态更改方法名称?

[英]Changing a method name dynamically in a loop?

I am actually struggling while trying to change a method name within a loop. 我实际上在尝试在循环中更改方法名称时遇到了麻烦。 I am building a GUI with QT and I am facing the problem of repeating the same function but with a different name. 我正在使用QT构建GUI,并且遇到了重复相同功能但使用不同名称的问题。

Here is my code: 这是我的代码:

from Config import Ui_Dialog

class Ui_Dialog_Useful(Ui_Dialog):

    def setupUi(self, widget):
        super().setupUi(widget)
        self.Sensitivity_filler()

    def Sensitivity_filler(self):
        self.Sensitivity_1.insertItems(0, ['10', '100', '1000'])
        self.Sensitivity_2.insertItems(0, ['10', '100', '1000'])
        self.Sensitivity_3.insertItems(0, ['10', '100', '1000'])
        self.Sensitivity_4.insertItems(0, ['10', '100', '1000'])
        self.Sensitivity_5.insertItems(0, ['10', '100', '1000'])
        self.Sensitivity_6.insertItems(0, ['10', '100', '1000'])
        self.Sensitivity_7.insertItems(0, ['10', '100', '1000'])
        self.Sensitivity_8.insertItems(0, ['10', '100', '1000'])

As you can see the only change between the functions is the name of the QComboBox object. 如您所见,函数之间的唯一变化是QComboBox对象的名称。 I have to perform this several times. 我必须执行几次。 Is there a way to do that within a loop? 有没有办法在一个循环中做到这一点?

Thanks 谢谢

Usually you shouldn't be doing this (ie dynamic names, since collections do this better), but since UI code tends to be tricky, you can use getattr to get the right object. 通常,您不应该这样做(例如,动态名称,因为集合可以更好地做到这一点),但是由于UI代码往往比较棘手,因此可以使用getattr来获取正确的对象。

For example, you'd probably do: 例如,您可能会这样做:

for i in range(1, 9):
    getattr(self, 'Sensitivity_'+str(i)).insertItems(0, ['10', '100', '1000'])

If there's a way of doing this without using getattr , you should definitely use it. 如果有一种不使用getattr ,则一定要使用它。 I'm not familiar with QT by any means. 我对QT一点都不熟悉。 For example, if it's possible, you should be storing your sensitivity items as an iterable (ie self.sensitivities ) and iterating over that instead. 例如,如果可能的话,您应该将敏感度项存储为可迭代的(即self.sensitivities ),然后对其进行迭代。

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

相关问题 动态更改函数__name__会引发AttributeError:'method'对象没有属性'__name__' - Dynamically changing a function __name__ throws AttributeError: 'method' object has no attribute '__name__' 在 Python 中动态更改列名 - Changing column name dynamically in Python 循环体是否改变了我的调用方法? (新白) - Is the loop body changing my call method? (NEWB) 如何打印动态分配函数的方法名称? - How to print method name of dynamically assigned function? 通过 python 应用循环更改列名和拆分文本 - Applying loop for changing columns name and split text through python 使用 for 循环在 panda 列中动态生成对象的名称 (fuzzywuzzy) - Dynamically generating an object's name in a panda column using a for loop (fuzzywuzzy) 通过在python的for循环中保存在var中的方法名称连接到类方法 - Connect to a Class Method by it's method name holded into a var in a for loop in python 在运行时确定名称/值的方法动态添加到类/实例的方法/状态 - Dynamically adding method/state to a class/instance with name/value determined at runtime 如何在循环中动态创建变量名以分配给 python 3 中的文件名 - How do I dynamically create a variable name in a loop to assign to a file name in python 3 Python-动态更改网址 - Python - Changing Url dynamically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM