gpt4 book ai didi

python - 通过 for 循环调用函数,函数作为字符串列表

转载 作者:太空宇宙 更新时间:2023-11-04 02:24:49 25 4
gpt4 key购买 nike

我正在尝试编写 PyQt5 应用程序。列表变量中的所有列表项都是表单的 LineEdit 对象。在调用重置函数时,我希望将它们设置为我试图为其编写 for 循环而不是硬编码的特定值。

但是,我不能将变量中的列表项用作对象,因为它们是其他文件中的对象属性并且在做

self.eval(variables[i]).setText(default_0_values[i])

不起作用,因为 eval 不是 self 的属性。但是,如果我运行

self.nGASLineEdit.setText(2)

有效。但我似乎无法自动化它,我被卡住了。需要解决这个问题。

from PyQt5 import QtCore,QtGui,QtWidgets,uic

variables=[
'nGASLineEdit','nDELTALineEdit','iMIPLineEdit','nDVECLineEdit','nSEEDLineEdit','eSTARTLineEdit','eTHRMLineEdit','eCUTLineEdit','nGAS1LineEdit','nGAS2LineEdit','nGAS3LineEdit','nGAS4LineEdit','nGAS5LineEdit','nGAS6LineEdit','fRAC1LineEdit','fRAC2LineEdit','fRAC3LineEdit','fRAC4LineEdit','fRAC5LineEdit','fRAC6LineEdit','tEMPLineEdit','tORRLineEdit','eFIELDLineEdit','bMAGLineEdit','bTHETALineEdit','iWRITELineEdit','iPENLineEdit','dETEFFLineEdit','eXCWGHTLineEdit','kGASLineEdit','lGASLineEdit','lCMPLineEdit','lRAYLineEdit','lPAPLineEdit','lBRMLineEdit','iECASCLineEdit'
]

default_0_values=[
2,100,5,1,0,1.0,1.5,2.0,
2 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
]

class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window,self).__init__()
uic.loadUi('main.ui',self)
self.pushButton_2.clicked.connect(lambda: self.reset())

def select_func(self,func_name):
return func_name

def reset(self):
global variables
global default_0_values
for i in range(len(variables)):
self.eval(variables[i]).setText(default_0_values[i])




if __name__=='__main__':
import sys
app=QtWidgets.QApplication(sys.argv)
window=Window()
window.show()
sys.exit(app.exec())

UI file in .ui format

UI file in .py format

最佳答案

使用 eval 是一种糟糕的编程习惯,在这种情况下是不必要的,除了放置 self.eval(...) 这些都表明 eval 是Window 类的一个函数,显然它不是。另一方面,如果您只读取变量,则没有必要使用 global。相反,我们可以使用 getattr(),如下所示:

from PyQt5 import QtCore,QtGui,QtWidgets,uic

variables=['nGASLineEdit','nDELTALineEdit','iMIPLineEdit','nDVECLineEdit','nSEEDLineEdit','eSTARTLineEdit','eTHRMLineEdit','eCUTLineEdit','nGAS1LineEdit','nGAS2LineEdit','nGAS3LineEdit','nGAS4LineEdit','nGAS5LineEdit','nGAS6LineEdit','fRAC1LineEdit','fRAC2LineEdit','fRAC3LineEdit','fRAC4LineEdit','fRAC5LineEdit','fRAC6LineEdit','tEMPLineEdit','tORRLineEdit','eFIELDLineEdit','bMAGLineEdit','bTHETALineEdit','iWRITELineEdit','iPENLineEdit','dETEFFLineEdit','eXCWGHTLineEdit','kGASLineEdit','lGASLineEdit','lCMPLineEdit','lRAYLineEdit','lPAPLineEdit','lBRMLineEdit','iECASCLineEdit']

default_0_values=[2,100,5,1,0,1.0,1.5,2.0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window,self).__init__()
uic.loadUi('main.ui',self)
self.pushButton_2.clicked.connect(self.reset)

def reset(self):
for le, val in zip(variables, default_0_values):
getattr(self, le).setText(str(val))

if __name__=='__main__':
import sys
app=QtWidgets.QApplication(sys.argv)
window=Window()
window.show()
sys.exit(app.exec())

关于python - 通过 for 循环调用函数,函数作为字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50671868/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com