PyQt5开发遇TypeError错误求助:参数1类型意外为QPushButton
Hey there! Let's figure out this error together—it's a simple fix once you spot the mistake.
The Root Cause
Take a look at this line in your Ui_Test class's setupUi method:
self.browseBtn.clicked.connect(self.browseBtn)
You accidentally connected the clicked signal of your browse button to the button itself instead of linking it to your browse_file_src method. PyQt expects a callable function/method as the signal handler, not a QPushButton object—that's exactly why you're getting that TypeError.
The Quick Fix
Change that line to connect to your file-browsing method instead:
self.browseBtn.clicked.connect(self.browse_file_src)
Bonus: Fixing Two Other Potential Issues
While we're at it, let's clean up a couple of small things that might cause unexpected behavior later:
Inconsistent Window Class Mismatch
YourUi_Testinherits fromQDialog, but in both your main window and the test block oftestGui.py, you're usingQMainWindowto instantiate it. This can lead to layout glitches. Switch to usingQDialogfor the second window:- In your main window's
open_test_windowmethod:self.window = QtWidgets.QDialog() # Changed from QMainWindow to QDialog self.ui = Ui_Test() self.ui.setupUi(self.window) self.ui.get_all_serials() self.window.show() - In the
__main__block oftestGui.py:Test = QtWidgets.QDialog() # Changed from QMainWindow to QDialog ui = Ui_Test() ui.setupUi(Test) Test.show() sys.exit(app.exec_())
- In your main window's
Redundant Method Call in
get_all_serials
You're callingself.serial.getAllSerial()twice—once without saving the result, then again to getreturn_list. Simplify it to a single call:def get_all_serials(self): self.serial = CameraID() return_list = self.serial.getAllSerial() # Only call once if len(return_list) > 0: for i in return_list: self.comboBox.addItem(i) else: self.comboBox.addItem("None")
Corrected Full Code for testGui.py
Here's the updated version with all fixes applied:
import sys import os from cameraID import CameraID from PyQt5.QtWidgets import QFileDialog, QDialog from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Test(QDialog): def browse_file_src(self): print('in the function') file_name = QFileDialog.getOpenFileName(self, 'Open file', '/home/qauser/') self.lineEdit.setText(file_name[0]) def get_all_serials(self): self.serial = CameraID() return_list = self.serial.getAllSerial() if len(return_list) > 0: for i in return_list: self.comboBox.addItem(i) else: self.comboBox.addItem("None") def setupUi(self, Test): Test.setObjectName("Test") Test.resize(397, 108) self.centralwidget = QtWidgets.QWidget(Test) self.centralwidget.setObjectName("centralwidget") self.lineEdit = QtWidgets.QLineEdit(self.centralwidget) self.lineEdit.setGeometry(QtCore.QRect(10, 10, 291, 21)) self.lineEdit.setObjectName("lineEdit") self.lineEdit.setPlaceholderText("Chose file") self.browseBtn = QtWidgets.QPushButton(self.centralwidget) self.browseBtn.setGeometry(QtCore.QRect(310, 10, 80, 23)) self.browseBtn.setObjectName("browseBtn") # Fixed signal connection here self.browseBtn.clicked.connect(self.browse_file_src) self.radioButton = QtWidgets.QRadioButton(self.centralwidget) self.radioButton.setGeometry(QtCore.QRect(10, 40, 51, 21)) self.radioButton.setObjectName("radioButton") self.radioButton_2 = QtWidgets.QRadioButton(self.centralwidget) self.radioButton_2.setGeometry(QtCore.QRect(60, 40, 71, 21)) self.radioButton_2.setObjectName("radioButton_2") self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(310, 40, 80, 23)) self.pushButton.setObjectName("pushButton") self.comboBox = QtWidgets.QComboBox(self.centralwidget) self.comboBox.setGeometry(QtCore.QRect(120, 40, 181, 23)) self.comboBox.setObjectName("comboBox") Test.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(Test) self.menubar.setGeometry(QtCore.QRect(0, 0, 397, 20)) self.menubar.setObjectName("menubar") Test.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(Test) self.statusbar.setObjectName("statusbar") Test.setStatusBar(self.statusbar) self.retranslateUi(Test) QtCore.QMetaObject.connectSlotsByName(Test) def retranslateUi(self, Test): _translate = QtCore.QCoreApplication.translate Test.setWindowTitle(_translate("Test", "MainWindow")) self.browseBtn.setText(_translate("Test", "Browse")) self.radioButton.setText(_translate("Test", "Ok")) self.radioButton_2.setText(_translate("Test", "Failed")) self.pushButton.setText(_translate("Test", "Start")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Test = QtWidgets.QDialog() # Changed to QDialog ui = Ui_Test() ui.setupUi(Test) Test.show() sys.exit(app.exec_())
Don't forget to update the open_test_window method in your main window code to use QDialog instead of QMainWindow for the second window.
After these changes, your browse button should work perfectly—opening the file picker and populating the line edit with the selected file path.
内容的提问来源于stack exchange,提问作者MBI

