Ad

Clicking A Button To Create A New Button Using PyQt5 Python

- 1 answer

I'm trying to create a window(UI) that have a button click action implemented using python and PyQt5. But, i want my button to create a new button when i clicked it. (i.e: clicking a button to create a new button). Can some one notify me on how to do that? my sample code is as follows:

    from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
    from PyQt5.QtGui import QIcon
    from PyQt5.QtCore import pyqtSlot
    import sys

    class App(QWidget):

    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle("my window")
        self.setGeometry(100, 100, 320, 200)

        #creating a button to be clicked
        button1 = QPushButton('Button-1', self)
        button1.move(100, 70)


        #calling a function on_click upon clicking button1, i want this function to create a new button named button2
        button.clicked.connect(self.on_click)
        self.show()

    @pyqtSlot()
    def on_click(self):
        print('Button-2 will be created')
        button2 = QPushButton('Button-2', self)
        button2.move(100, 200)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())

I want my function, the on_click(self), to create a new button: but i can't. do i miss something?

Ad

Answer

You need to replace button.clicked.connect (self.on_click) with button1.clicked.connect (self.on_click). and add button2.show ().

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot


class App(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle("my window")
        self.setGeometry(100, 100, 320, 300)

        #creating a button to be clicked
        button1 = QPushButton('Button-1', self)
        button1.move(100, 70)
        button1.clicked.connect(self.on_click)      # button1

    @pyqtSlot()
    def on_click(self):
        print('Button-2 will be created')
        button2 = QPushButton('Button-2', self)
        button2.move(100, 200)
        button2.show()                              # +++

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    ex.show()
    sys.exit(app.exec_())

enter image description here


Or using layouts:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot


class App(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle("my window")
        self.num = 2

        #creating a button to be clicked
        button1 = QPushButton('Button-1', self)
#        button1.move(100, 70)
        button1.clicked.connect(self.on_click)     

        self.layout = QVBoxLayout(self) 
        self.layout.addWidget(button1)        

    @pyqtSlot()
    def on_click(self):
        print('Button-{} will be created'.format(self.num))
        button2 = QPushButton('Button-{}'.format(self.num), self)
        button2.clicked.connect(lambda : print(button2.text()))
#        button2.move(100, 200)

        self.layout.addWidget(button2)
        self.num += 1


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    ex.show()
    sys.exit(app.exec_())

enter image description here

Ad
source: stackoverflow.com
Ad