введите сюда код
[![在此输入图像描述][1]][1]需要调用一个方法,例如:
self.container.font.setPointSize(10)
但是您需要通过知道名称(动态获取)来做到这一点
通过以下方式尝试过:
command_parent = getattr(self.container, parent.text())
command = getattr(command_parent(), command_name_parameter)
command(10)
在这种情况下,什么也没有发生,方法本身被获取,但不被执行。
Exec 也不起作用,它给出了语法错误(我知道这不安全)
command = f"{self.container}.{parent.text()}().set{ property[:1].upper() + property[1:]}(10)"
print(command)
exec(command)
File "<string>", line 1
<PySide6.QtWidgets.QPushButton(0x1e67421dd80) at 0x000001E6757D22C0>.font().setPointSize(10)
^
SyntaxError: invalid syntax
我能做什么或者我在代码中的某个地方犯了错误吗?
数据如何到达的示例,有很多这样的属性,因此手动输入所有命令既困难又耗时:
{
"name": "font",
"type": "QFont",
"widget_type": "QWidget",
"parameters": [
{
"name": "family",
"type": "QString"
},
{
"name": "PointSize",
"type": "int"
},
{
"name": "bold",
"type": "bool"
},
{
"name": "italic",
"type": "bool"
},
{
"name": "underline",
"type": "bool"
},
{
"name": "strikeout",
"type": "bool"
},
{
"name": "kerning",
"type": "bool"
}
]
}
如果我理解正确的话:
结论: