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

PyQt5开发遇TypeError错误求助:参数1类型意外为QPushButton

Fixing the TypeError: argument 1 has unexpected type 'QPushButton' in Your PyQt5 GUI

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:

  1. Inconsistent Window Class Mismatch
    Your Ui_Test inherits from QDialog, but in both your main window and the test block of testGui.py, you're using QMainWindow to instantiate it. This can lead to layout glitches. Switch to using QDialog for the second window:

    • In your main window's open_test_window method:
      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 of testGui.py:
      Test = QtWidgets.QDialog()  # Changed from QMainWindow to QDialog
      ui = Ui_Test()
      ui.setupUi(Test)
      Test.show()
      sys.exit(app.exec_())
      
  2. Redundant Method Call in get_all_serials
    You're calling self.serial.getAllSerial() twice—once without saving the result, then again to get return_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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 19:08:12