Why Isn't QPushButton Triggering Method in PyQt5?

  • Context: Python 
  • Thread starter Thread starter Mckamar
  • Start date Start date
  • Tags Tags
    Gui Python
Click For Summary
SUMMARY

The forum discussion centers on a user experiencing issues with a QPushButton in a PyQt5 application not triggering the method add_Bladebox. The user has implemented the button correctly but reports that the method does not execute when the button is clicked. The discussion reveals that the method works when called directly in the code, indicating that the connection to the button click event is likely the source of the problem. A common mistake highlighted is the incorrect use of parentheses when connecting the button's click signal to the method.

PREREQUISITES
  • Understanding of PyQt5 framework and its widgets
  • Familiarity with signal-slot mechanism in PyQt5
  • Basic knowledge of Python programming
  • Experience with GUI development concepts
NEXT STEPS
  • Review PyQt5 signal-slot connections and ensure correct syntax
  • Learn about QPushButton properties and methods in PyQt5
  • Explore debugging techniques for PyQt5 applications
  • Investigate dynamic widget creation in PyQt5, specifically using QDoubleSpinBox
USEFUL FOR

Python developers, particularly those working with PyQt5 for GUI applications, and anyone troubleshooting button click events in their PyQt5 projects.

Mckamar
Messages
1
Reaction score
0
I'm new to this forum, as well as Python, so bare with me, won't you:)

Ok, so I have this GUI written in python and utilizing PYQT5. I have a functioning version, but I now want to add a QpushButton that executes a method called add_Bladebox. This method adds a doublespinbox to the gui. The method works fine when I call it as part of the code, but when I try to execute it via the QpushButton, nothing happens. I know that without a loop, I could only ever do it once anyway(no position change implemented), but I can add that feature later. So any suggestion as to why it's not working is well appreciated. Thanks in advance!

Python:
[code=Python]import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QPushButton, QSizePolicy,

                      QDoubleSpinBox, QLabel, QCheckBox, QMainWindow,
QGridLayout)
from PyQt5.QtCore import QCoreApplication
import matplotlib
from matplotlib.figure import Figure
import numpy as np

# Mark: QGridlayout importoitu, muttei käytetty

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as F
igureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as
NavigationToolbarclass MainWindow(QWidget):

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

   self.initUI()

   # Geometry of main window:
   self.setGeometry(200, 200, 1000, 1000)
   self.setWindowTitle('Simulation')   # Set canvas:
   self.m = PlotCanvas(self, width=5, height=10)
   self.m.move(0,0)   # Simulate button:
   sim = QPushButton('Simulate', self)
   sim.clicked.connect(self.simulate)
   sim.resize(sim.sizeHint())
   sim.move(550, 700)

   # Quit button:
   qbtn = QPushButton('Quit', self)
   qbtn.clicked.connect(QCoreApplication.instance().quit)
   qbtn.resize(qbtn.sizeHint())
   qbtn.move(800, 700)

   # Input box for x1:
   self.x1 = self.inputBox(800,80,10.0,0.01,0.2,2)
   self.x1_label = self.label(800,60,"x1")   # Checkbox for sending data via email:
   self.send_box = QCheckBox('Send data’, self)
   self.send_box.move(550, 600)
   self.send_box.toggle()   # Button for adding input boxes
   blade_button = QPushButton('Add', self)
   # THE PROBLEM IS THE NEXT LINE AS FAR AS I UNDERSTAND
   blade_button.clicked.connect(self.add_Bladebox)

   self.show()

# Method executes simulation:
def simulate(self):

   # TODO Read parameter values. Look explanations of parameters from
simu_func.py
# Method for input box:
def inputBox(self, left, top, maxvalue, step, default,decimals):
   box = QDoubleSpinBox(self)
   box.move(left,top)
   box.setDecimals(decimals)
   box.setMaximum(maxvalue)
   box.setSingleStep(step)
   box.setProperty("value", default)
   box.resize(box.sizeHint())
   return box# Method for label:
def label(self, left, top, text):
   lbl = QLabel(self)
   lbl.setText(text)
   lbl.resize(lbl.sizeHint())
   lbl.move(left,top)
   return lbl

# Method for adding blade boxes
def add_Bladebox(self):
   left=900
   top=500
   maxvalue=3
   step=1
   default=0
   decimals=1
   blade_box = self.inputBox(left, top, maxvalue, step, default, decimals)
[/code]
 
Technology news on Phys.org
I'm not 100% certain, but I think the problem is that the line you have that reads:

sim.clicked.connect(self.simulate)

should read as follows in order to call the simulate() method.

sim.clicked.connect(self.simulate())
 
Mckamar said:
so bare with me, won't you:)
I prefer to keep my clothes on, thank you... :-p

Sorry, I couldn't resist. I've seen this typo so many times (hopefully just from errant spell checkers?) and finally gave into temptation!
 
Last edited:

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 0 ·
Replies
0
Views
2K