Why Isn't QPushButton Triggering Method in PyQt5?

  • Python
  • Thread starter Mckamar
  • Start date
  • Tags
    Gui Python
In summary, the problem is that you are trying to call the simulate() method on the QpushButton, but you are missing the .connect() keyword.
  • #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:

1. What could be causing my GUI to freeze or crash?

This could be due to a variety of reasons, such as inefficient coding, memory leaks, or compatibility issues with the operating system or hardware. It is important to analyze your code and use debugging tools to identify the specific cause of the issue.

2. How can I improve the user experience of my GUI?

To improve the user experience of your GUI, you can focus on making the interface intuitive and user-friendly, optimizing the layout and design, and implementing efficient and responsive interactions. User testing and feedback can also help identify areas for improvement.

3. Is it possible to add animations or visual effects to my GUI?

Yes, it is possible to add animations and visual effects to your GUI using programming libraries or frameworks that support these features. However, it is important to use them sparingly and make sure they do not interfere with the functionality of the GUI.

4. How can I make my GUI compatible with different screen resolutions and sizes?

To make your GUI compatible with different screen resolutions and sizes, you can use responsive design techniques, such as fluid layouts and relative positioning, to ensure that the elements of your GUI adjust accordingly. You can also test your GUI on different devices to ensure compatibility.

5. Can I customize the appearance and styling of my GUI?

Yes, you can customize the appearance and styling of your GUI by using CSS or other styling languages. This allows you to change the colors, fonts, and layout of your GUI to match your desired design aesthetic and branding.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
10
Views
10K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
7
Views
6K
  • Programming and Computer Science
Replies
1
Views
3K
Back
Top