[英]PyQt5 TabWidget tabBarClicked TypeError: native Qt signal is not callable
我正在尝试使用 PyQt5 制作用户界面。 如果我单击第 5 个索引选项卡 userSettings() 函数将调用。 但是程序引发了这个错误:
self.tabWidget.tabBarClicked(5).connect(self.userSettings())
TypeError: native Qt signal is not callable
我该如何解决?
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)
您的代码中有两个问题:
None
引发 TypeError ;改成:
self.tabWidget.tabBarClicked.connect(self.userSettings)
进而:
def userSettings(self, tabIndex):
if tabIndex != 5:
return
# ...
请注意,您应该连接到currentChanged
信号,而不是tabBarClicked
信号,因为即使单击的选项卡已经是当前选项卡,它也会被触发。
显然tabWidget.tabBarClicked
是不可调用的。
我会将侦听器连接到tabWidget
的onchange
:
self.tabWidget.currentChanged.connect(self.function_to_run)
然后你可以写你的function_to_run
来检查currentIndex()
...
self.tabs.currentChanged.connect(self.function_to_run)
...
def function_to_run(self):
if self.tabWidget.currentIndex() == 5:
print("Do stuff")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.