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


Messages In This Thread
pyqt clickable pushbutton problem - by pythonck - Dec-09-2017, 06:44 PM
RE: pyqt clickable pushbutton problem - by pythonck - Dec-12-2017, 03:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Clickable Rectangles Tkinter Canvas MrTim 4 9,097 May-11-2021, 10:01 PM
Last Post: MrTim
  [Tkinter] Glow text of clickable object on hover with transition andy 6 6,302 May-11-2021, 07:39 AM
Last Post: andy
  [PyGUI] Pushbutton in ANSA Aishaf 2 2,664 Apr-27-2021, 11:22 AM
Last Post: Aishaf
  [PyQt] pushbutton with image issac_n 1 1,823 Jul-13-2020, 05:04 PM
Last Post: Knight18
  [PyQt] Add command to PushButton skaailet 1 1,749 Apr-11-2020, 01:46 PM
Last Post: deanhystad
  Making text clickable with binding DT2000 10 5,309 Apr-02-2020, 10:11 PM
Last Post: DT2000
  TypeError when using PushButton (PyQt5) lmsavk 1 6,856 Mar-03-2019, 04:21 PM
Last Post: Alfalfa
  (pyQt/pySide)setStyleSheet(border…) makes QPushButton not clickable in Maya vladlenPy 0 4,788 Apr-15-2018, 12:41 PM
Last Post: vladlenPy
  [PyQt] QTreeView branches and their clickable area not coinciding abstracted 0 3,801 Mar-17-2017, 02:28 AM
Last Post: abstracted
  keeping track of pushbutton click iFunKtion 3 4,344 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