Python Forum

Full Version: how to dynamically add objects to pyqt window
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need to be able to dynamically add an object like a label to my window. How do I do that? Here is the code that I have.
class Main(QtGui.QMainWindow, stuff):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.resize(600, 500)
        self.setup()
        
    def keyPressEvent(self, event):
        if event.key() == QtCore.Qt.Key_A:
            self.addblock()

    def addblock(self):
        self.newblock = QtGui.QLabel(self.frame)
        self.newblock.setGeometry(QtCore.QRect(10, 10, 40, 40))
        self.newblock.setPixmap(self.blockimg)#I use the label to display an image.
     
The problem is when I push the A key I know that the new label is being created but it will not show on the window. How do I get the label to show on the window?
That depends on which layout manager you are using. There is a nice tutorial here

http://zetcode.com/gui/pyqt5/layout/