Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PyQt5 TabWidget tabBarClicked TypeError: native Qt signal is not callable

I am trying to making a user interface with PyQt5. If i click the 5th index tab userSettings() function will call. But program raises this error:

self.tabWidget.tabBarClicked(5).connect(self.userSettings())
TypeError: native Qt signal is not callable

How can i fix it?

import sys
import PyQt5.QtCore
from PyQt5.QtWidgets import QApplication, QMainWindow, QTabWidget, QTabBar, QWidget
from PyQt5.QtWidgets import QMessageBox
from numpy import *
from alfa_gui import Ui_MainWindow
    
class MainWindow(QMainWindow, Ui_MainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.tabWidget.tabBarClicked(5).connect(self.userSettings())

    def userSettings(self):
        
        if self.lineEdit.text() == "cogal" and self.lineEdit_2.text() == "cogal":
            print("Success!")
        else:
            msg = QMessageBox()
            msg.setWindowTitle("Hatalı Giriş!")
            msg.setText("Wrong Password Or Username")
            x = msg.exec_()  
            msg.setIcon(QMessageBox.Critical)

    if __name__ == '__main__':
        app = QApplication(sys.argv)
        window = MainWindow()
        window.setWindowTitle('ALFA')
        window.show()
        exit_code = app.exec_()
        sys.exit(exit_code)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There are two problems in your code:

  1. the arguments shown in the signals documentation are the arguments received from the functions they’re connected to;
  2. signal connections require the reference to a callable, while you’re calling the desired function (with the parentheses), which would raise a TypeError as the function returns None;

Change to:

    self.tabWidget.tabBarClicked.connect(self.userSettings)

And then:

def userSettings(self, tabIndex):
    if tabIndex != 5:
        return
    # ...

Note that you should connect to the currentChanged signal, not the tabBarClicked one, as that will be triggered anyway even if the clicked tab is already the current one.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading