More

Align scale bar with PyQGIS

Align scale bar with PyQGIS


I am making several maps from a template with a PyQGIS script and I am having problems to align the scale bar on them. My code is:

# create scale bar item = QgsComposerScaleBar(comp) item.setStyle('Single Box') # Other possibilities are: 'Single Box', 'Double Box', 'Line Ticks Middle', 'Line Ticks Down', 'Line Ticks Up', 'Numeric' item.setComposerMap(composerMap) item.applyDefaultSize() item.setAlignment('Right') comp.addItem(item)

I've found theQgsComposerScaleBar.setAlignment()QGIS API Documentation, but when I introduce 'Right' as a parameter I get a type error message. I am using QGIS 2.6.0


ThesetAlignment()method accepts the enumaratorQgsComposerScaleBar::Alignment. So you should change your code as following:

item.setAlignment(QgsComposerScaleBar.Right)

Hope this helps.


Watch the video: 2019 - PyQGIS the comfortable way - tricks to efficiently work with Python and QGIS