0

Fairly new to Python and very new to PyQt5. Getting an error message when trying to assign a method to a button. Not sure what I'm doing different compared to everyone else on the internet.

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys


class MyWindow(QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.setGeometry(0, 0, 720, 500)
        self.setWindowTitle("PQ Visualizer")
        self.initUI()

    def initUI(self):
        self.label = QtWidgets.QLabel(self)
        self.label.setText("Label 1")
        self.label.move(50, 50)

        self.b1 = QtWidgets.QPushButton(self)
        self.b1.setText("Button 1")
        self.b1.clicked.connect(self.clicked())

    def clicked(self):
        self.label.setText("You pressed the button.")


def window():
    app = QApplication(sys.argv)
    win = MyWindow()
    win.show()
    sys.exit(app.exec_())


window()

Error Message

Trying to assign a simple function to a QPushButton

1 Answers1

1

You are supposed to pass the function, not call it, so remove the ():

self.b1.clicked.connect(self.clicked())

should be

self.b1.clicked.connect(self.clicked)
DeepSpace
  • 78,697
  • 11
  • 109
  • 154