转载自:Pyqt 动态的添加控件 - dcb3688 - 博客园
https://www.cnblogs.com/dcb3688/p/4588814.html
# -*- coding: utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
'''
Pyqt 动态的添加控件
'''
class DynAddObject(QDialog):
def __init__(self, parent=None):
super(DynAddObject, self).__init__(parent)
addButton = QPushButton(u"添加控件")
self.layout = QGridLayout()
self.layout.addWidget(addButton, 1, 0)
self.setLayout(self.layout)
self.connect(addButton, SIGNAL("clicked()"), self.add)
def add(self):
btncont= self.layout.count()
widget = QPushButton(str(btncont), self)
self.layout.addWidget(widget)
if __name__ == "__main__":
app = QApplication(sys.argv)
form = DynAddObject()
form.show()
app.exec_()
class DynAddObject(QDialog):
def __init__(self, parent=None):
super(DynAddObject, self).__init__(parent)
addButton = QPushButton(u"添加控件")
self.layout = QGridLayout()
self.layout.addWidget(addButton, 1, 0)
self.setLayout(self.layout)
self.add()
def add(self):
self.button={}
print(type(self.button))
for i in range(1, 8):
self.button[i]=QPushButton(str(i))
self.layout.addWidget(self.button[i])
if __name__ == "__main__":
app = QApplication(sys.argv)
form = DynAddObject()
form.show()
app.exec_()