Dec-09-2017, 06:44 PM
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_())