[英]PyQt5 TabWidget tabBarClicked TypeError: native Qt signal is not callable
I am trying to making a user interface with PyQt5.我正在尝试使用 PyQt5 制作用户界面。 If i click the 5th index tab userSettings() function will call.
如果我单击第 5 个索引选项卡 userSettings() 函数将调用。 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)
There are two problems in your code:您的代码中有两个问题:
None
;None
引发 TypeError ; 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.请注意,您应该连接到
currentChanged
信号,而不是tabBarClicked
信号,因为即使单击的选项卡已经是当前选项卡,它也会被触发。
Clearly the tabWidget.tabBarClicked
is not callable.显然
tabWidget.tabBarClicked
是不可调用的。
I would connect a listener to tabWidget
's onchange
:我会将侦听器连接到
tabWidget
的onchange
:
self.tabWidget.currentChanged.connect(self.function_to_run)
Then you can write your function_to_run
to check the currentIndex()
然后你可以写你的
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.