【问题标题】:Create a grid of buttons using a loop in PyQT4在 PyQT4 中使用循环创建按钮网格
【发布时间】:2012-01-23 04:28:39
【问题描述】:

有没有办法在 PyQT4 中使用循环创建按钮网格?

例如,具有以下效果的东西:

for j in range(0, 10):
    for k in range(0, 10):
        grid.addbutton(j, k)

谢谢。

【问题讨论】:

    标签: python button grid pyqt pyqt4


    【解决方案1】:

    当然有。 QGridLayout 在这种情况下可能有用。

    这是一个最小的例子:

    import sys
    from PyQt4 import QtGui
    
    app = QtGui.QApplication(sys.argv)
    widget = QtGui.QWidget()
    layout = QtGui.QGridLayout()
    
    buttons = {}
    
    for i in range(10):
        for j in range(10):
            # keep a reference to the buttons
            buttons[(i, j)] = QtGui.QPushButton('row %d, col %d' % (i, j))
            # add to the layout
            layout.addWidget(buttons[(i, j)], i, j)
    
    widget.setLayout(layout)
    widget.show()
    sys.exit(app.exec_())
    

    结果:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 2018-02-01
      • 1970-01-01
      • 2013-03-06
      • 2014-10-20
      相关资源
      最近更新 更多