Python Forum
pyqt clickable pushbutton problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pyqt clickable pushbutton problem
#1
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_())
Reply
#2
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_())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Clickable Rectangles Tkinter Canvas MrTim 4 8,853 May-11-2021, 10:01 PM
Last Post: MrTim
  [Tkinter] Glow text of clickable object on hover with transition andy 6 6,064 May-11-2021, 07:39 AM
Last Post: andy
  [PyGUI] Pushbutton in ANSA Aishaf 2 2,604 Apr-27-2021, 11:22 AM
Last Post: Aishaf
  [PyQt] pushbutton with image issac_n 1 1,773 Jul-13-2020, 05:04 PM
Last Post: Knight18
  [PyQt] Add command to PushButton skaailet 1 1,699 Apr-11-2020, 01:46 PM
Last Post: deanhystad
  Making text clickable with binding DT2000 10 5,127 Apr-02-2020, 10:11 PM
Last Post: DT2000
  TypeError when using PushButton (PyQt5) lmsavk 1 6,740 Mar-03-2019, 04:21 PM
Last Post: Alfalfa
  (pyQt/pySide)setStyleSheet(border…) makes QPushButton not clickable in Maya vladlenPy 0 4,730 Apr-15-2018, 12:41 PM
Last Post: vladlenPy
  [PyQt] QTreeView branches and their clickable area not coinciding abstracted 0 3,708 Mar-17-2017, 02:28 AM
Last Post: abstracted
  keeping track of pushbutton click iFunKtion 3 4,265 Mar-13-2017, 12:18 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020