You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyQt5绑定tabBarClicked报native Qt signal is not callable如何解决

PyQt5标签页点击触发函数报错修复方案

错误原因

你的代码存在两处核心错误:

  • Qt信号tabBarClicked本身不可直接传参调用,你写的tabBarClicked(5)属于错误用法,这是触发TypeError: native Qt signal is not callable的直接原因。该信号触发时会自动将被点击标签的索引作为参数传递给绑定的槽函数。
  • 绑定槽函数时不能加括号(),加括号会在类初始化阶段直接执行一次目标函数,无法实现点击触发的效果。
    另外你原有代码中QMessageBox的图标设置写在了弹窗执行语句之后,会导致图标不显示,也需要调整顺序。

修复代码

1. 修改初始化中的信号绑定逻辑,调整槽函数实现

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.connect(self.userSettings)

    def userSettings(self, index):
        # 仅当点击索引为5的标签页时执行后续逻辑
        if index != 5:
            return
        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")
            # 调整图标设置顺序,放在弹窗执行前
            msg.setIcon(QMessageBox.Critical)
            x = msg.exec_()  

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

内容的提问来源于stack exchange,提问作者neyimvarki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 10:06:03