Why Isn't QPushButton Triggering Method in PyQt5?

  • Python
  • Thread starter Mckamar
  • Start date
  • Tags
    Gui Python
  • #1
Mckamar
1
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
  • #2
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())
 
  • #3
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
Views
1K
Replies
1
Views
3K
Replies
1
Views
2K
Replies
4
Views
2K
Replies
16
Views
2K
Replies
8
Views
2K
Replies
8
Views
2K
Replies
11
Views
2K
Replies
2
Views
2K
Replies
0
Views
269
Back
Top