Home > Back-end >  PyQt5 TabWidget tabBarClicked TypeError: native Qt signal is not callable
PyQt5 TabWidget tabBarClicked TypeError: native Qt signal is not callable

Time:11-09

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)

CodePudding user response:

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.

CodePudding user response:

Clearly the tabWidget.tabBarClicked is not callable.

I would connect a listener to tabWidget's onchange:

self.tabWidget.currentChanged.connect(self.function_to_run)

Then you can write your function_to_run to check the currentIndex()

...
self.tabs.currentChanged.connect(self.function_to_run)
...
def function_to_run(self):
    if self.tabWidget.currentIndex() == 5:
        print("Do stuff")
  • Related