Python Forum

Full Version: pyqt clickable pushbutton problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
When the pushbutton in my program is clicked it runs a function which should produce another pushbutton. The function runs as evidenced by the "clicked" message it produces in the console but it doesn't produce the new button. However, the same function produces the new button when I run it from the main function (by uncommenting the line commented in the main function). Why doesn't the clicked button produce a new button with the function it runs?

  
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
     def setupUi(self, Form):
         Form.setObjectName("Form")
         Form.resize(400, 300)
         Form.move(1200,300)
     def setup_pushButton1(self, word, x, y, width, height):
         self.pushButton1 = QtWidgets.QPushButton(Form)
         self.pushButton1.setGeometry(QtCore.QRect(x, y, width, height))
         self.pushButton1.setText(word)
         self.pushButton1.clicked.connect(self.but_clicked)
     def create_pushButton1(self):
         self.setup_pushButton1('apple', 20, 110, 75, 23)
     def but_clicked(self):
         self.pushButton2 = QtWidgets.QPushButton(Form)
         self.pushButton2.setGeometry(QtCore.QRect(250, 110, 75, 23))
         self.pushButton2.setText('house')
         print('hello')

if __name__ == "__main__":
     import sys
     app = QtWidgets.QApplication(sys.argv)
     Form = QtWidgets.QWidget()
     ui = Ui_Form()
     ui.setupUi(Form)
     ui.create_pushButton1()
     # ui.but_clicked()
     Form.show()
     sys.exit(app.exec_())
Actually, the new button is created. It just didn't show. By adding the line -- self.pushButton.show() -- it now shows. Here's the revised code that works.

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
     def setupUi(self, Form):
         Form.setObjectName("Form")
         Form.resize(400, 300)
     def setup_pushButton(self, word, x, y, width, height):
         self.pushButton = QtWidgets.QPushButton(Form)
         self.pushButton.setGeometry(QtCore.QRect(x, y, width, height))
         self.pushButton.setText(word)
         self.pushButton.clicked.connect(self.but_clicked)
     def create_pushButtons(self):
         self.setup_pushButton('apple', 100, 110, 75, 23)
         self.setup_pushButton('car', 20, 110, 75, 23)
     def but_clicked(self):
         print('clicked')
         self.setup_pushButton('house', 250, 110, 75, 23)
         self.pushButton.show()
if __name__ == "__main__":
     import sys
     app = QtWidgets.QApplication(sys.argv)
     Form = QtWidgets.QWidget()
     ui = Ui_Form()
     ui.setupUi(Form)
     ui.create_pushButtons()
     Form.show()
     sys.exit(app.exec_())
[/python
[hr]
Actually, the new button is created.  It just didn't show.  By adding the line -- self.pushButton.show() -- it now shows.  Here's the revised code that works.

[python]
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
     def setupUi(self, Form):
         Form.setObjectName("Form")
         Form.resize(400, 300)
     def setup_pushButton(self, word, x, y, width, height):
         self.pushButton = QtWidgets.QPushButton(Form)
         self.pushButton.setGeometry(QtCore.QRect(x, y, width, height))
         self.pushButton.setText(word)
         self.pushButton.clicked.connect(self.but_clicked)
     def create_pushButtons(self):
         self.setup_pushButton('apple', 100, 110, 75, 23)
         self.setup_pushButton('car', 20, 110, 75, 23)
     def but_clicked(self):
         print('clicked')
         self.setup_pushButton('house', 250, 110, 75, 23)
         self.pushButton.show()
if __name__ == "__main__":
     import sys
     app = QtWidgets.QApplication(sys.argv)
     Form = QtWidgets.QWidget()
     ui = Ui_Form()
     ui.setupUi(Form)
     ui.create_pushButtons()
     Form.show()
     sys.exit(app.exec_())