我正在嘗試使用 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)
uj5u.com熱心網友回復:
您的代碼中有兩個問題:
- 信號檔案中顯示的引數是從它們所連接的函式接收的引數;
- 信號連接需要對可呼叫的參考,而您正在呼叫所需的函式(帶括號),這會在函式回傳時引發 TypeError
None;
改成:
self.tabWidget.tabBarClicked.connect(self.userSettings)
進而:
def userSettings(self, tabIndex):
if tabIndex != 5:
return
# ...
請注意,您應該連接到currentChanged信號,而不是信號,tabBarClicked因為即使單擊的選項卡已經是當前選項卡,它也會被觸發。
uj5u.com熱心網友回復:
顯然tabWidget.tabBarClicked是不可呼叫的。
我會將偵聽器連接到tabWidget's 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")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/352674.html
標籤:Python qt pyqt pyqt5 qt-设计师
下一篇:將Pandas類別分配給新號碼
