python - PyQt 中的布局由 QWidgetItems 而不是 QPushButtons 组成,并且没有属性文本

标签 python button layout pyqt

我创建了一个布局并通过 for 循环将 4 个按钮放在那里。

layout = QtGui.QVBoxLayout()
my_list = ['1', '2', '3', '4']
for elem in my_list:
        button = QtGui.QPushButton(elem)
        layout.addWidget(button)

然后我想检查是否有任何按钮具有我在变量 VAR 中确定的文本。如果是这样 - 做点什么。

items = (layout.itemAt(i) for i in range(layout.count())) #get list of items in lay

for w in items:
    print w
    #it gives <PyQt4.QtGui.QWidgetItem object at 0x031F65D0> * 4 
    #instead of QPushButton.

    VAR = '1'
    if w.text() == VAR : #Problem here.
        print 'I got what I want.'
        #the problem is that QWidgetItem has no atribute text.

请告诉我如何获取 QPushButtons 列表而不是 QWidgetItems 或针对此问题的任何其他解决方案。

最佳答案

QWidgetItem 的文档很清楚。你只需要做:

for item in items:
    w = item.widget()

关于python - PyQt 中的布局由 QWidgetItems 而不是 QPushButtons 组成,并且没有属性文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40469368/

相关文章:

python - 使用sklearn线性回归,如何限制计算的回归系数大于0?

python - 如何在运行时访问未知对象 - 而不是它们的名称

python - Ubuntu Modoboa 从 NGINX 到 APACHE2 : HttpS 403 forbidden while HTTP is correctly accessible. (mod_WSGI)

python3通过分隔符将大文件分割成小文件(不是大小,行)

html - 我如何将带有 Logo 的 HTML DIV 居中放置在左侧?

Android - 在默认按钮上保持背景图像的纵横比

java - 在 XML 中设置 Button View 的背景会使按钮不可单击

javascript - X 单击后如何禁用按钮?

android - Android 上的横向按钮菜单

android - 如何使用引用父 View 的资源?