基于子字符串查找Ui_形式的变量

2024-05-14 07:02:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个来自QtDesigner的Ui_表单,其中包含变量名,例如:

self.ui.alphabetic_menu_QLabel_A
...
self.ui.alphabetic_menu_QLabel_Z
self.ui.label
...
self.ui.label_10

我发现Ui_表单是不可移植的。是否有一种好的Python方法可以将所有实例提取到包含指定子字符串的变量名列表中(例如,字母菜单标签)


Tags: 实例方法字符串selfui表单列表字母
1条回答
网友
1楼 · 发布于 2024-05-14 07:02:17

我不知道QtDesigner,但是如果您处理的是python实例,那么可以使用dir()内置方法来完成

dir(yourform.ui)返回所有属性名称的列表。从你的例子中,我认为你应该得到类似的东西:['alphabetic_menu_QLabel_A', ..., 'alphabetic_menu_QLabel_Z', 'label', ..., 'label_10', ...]

小心点,因为dir()也会返回其他内容。从文档中:

the list contains the object’s attributes’ names, the names of its class’s attributes, and recursively of the attributes of its class’s base classes.

所以你需要过滤列表。如果您对只包含给定子字符串的属性感兴趣,可以使用列表理解

mysubstr = 'alphabetic_menu_QLabel'
myattributes = [att for att in dir(yourform.ui) if mysubstr in att]

相关问题 更多 >

    热门问题