将字符串解释为变量名(涉及类)- Python
我有一些变量名称字符串,它们最初必须是字符串(由于文件 IO)。
这些对应的变量是 PyQt4 小部件的变量名称,嵌入两个类中。
我需要这些字符串能够转换为实际的变量名称,以便在函数中使用它们。
(有点像你如何“int”一个数字字符串)
这是代码(显示班级疯狂)
第一个文件
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.VariableName = QWidget(whatever)
第二个文件
from FIRST_FILE import Ui_MainWindow
class Start(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def ResetDropBoxes(self):
X = "VariableName"
self.ui.X.Function()
有些人可能会认为这是Pyuic4(Qt Designer)生成的python代码的格式
第二个文件的最后两行是麻烦所在。 我收到错误消息“Ui_MainWindow() 没有属性‘X’”。
除了尝试“eval”之外,我不知道该去哪里。 我尝试了诸如此类的操作...
X = self.ui.eval("VariableName")
这提示错误“Ui_MainWindow()没有属性'eval'
X = eval("VariableName")
“VariableName未定义”
X = "VariableName"
eval(self.ui.X.Function())
“Ui_MainWindow()没有属性'X'”
我可以使用任何和所有帮助来解决这个问题!< br> 我觉得 eval 让我更近了一步,但是使用这些嵌套类会抑制它!
哈尔普'?!
详情:
- Python 2.7.1
- Windows 7(32 位)
- 空闲 1.8
- PyQt4
- Qt 设计师
I have some strings of variables names, that must initially be strings (due to file IO).
The variables these correspond to are variable names of PyQt4 widgets, embed in two classes.
I need these strings able to be converted to the actual variable names to use them in functions.
(Kind of like how you 'int' a string of a number)
Here's the code (Showing the Class Madness)
FIRST FILE
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.VariableName = QWidget(whatever)
SECOND FILE
from FIRST_FILE import Ui_MainWindow
class Start(QtGui.QMainWindow):
def __init__(self, parent = None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
def ResetDropBoxes(self):
X = "VariableName"
self.ui.X.Function()
Some might recognise this as the format of python code generated by Pyuic4 (Qt Designer)
The last two lines of the Second File are the trouble.
I get the error message "Ui_MainWindow() has no attribute 'X'".
I had no idea where to go with this, except an attempt with 'eval'.
I tried things such as...
X = self.ui.eval("VariableName")
This prompted errors "Ui_MainWindow() has no attribute 'eval'
X = eval("VariableName")
"VariableName is not defined"
X = "VariableName"
eval(self.ui.X.Function())
"Ui_MainWindow() has no attribute 'X'"
I could use any and all help for this matter!
I feel that eval gets me a step closer, but using these nested classes inhibits it!
HALP'?!
DETAILS:
- Python 2.7.1
- Windows 7 (32 bit)
- IDLE 1.8
- PyQt4
- Qt Designer
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请尝试
参阅此处,了解有关
getattr
的更多信息。Try
See here for more info on
getattr
.