Pyqt4错误消息:意外的类型'str'和参数不足

时间:2019-04-01 13:13:55

标签: python string pyqt4

我得到

TypeError: arguments did not match any overloaded call:" 

当我运行我的Pyqt4程序时,该消息应该会在单击添加,编辑或删除菜单项时调用其他微型窗口,我在做什么错了

确切的错误消息是

Traceback (most recent call last):
  File "C:/Python27/Lib/site-packages/PyQt4/mytry.py", line 27, in <module>
    add=QAction("Add new record")
TypeError: arguments did not match any overloaded call:
  QAction(QObject): argument 1 has unexpected type 'str'
  QAction(QString, QObject): not enough arguments
  QAction(QIcon, QString, QObject): argument 1 has unexpected type 'str'

这是mytry.py

import sys
from PyQt4.QtGui import *

# Create an PyQT4 application object.
a = QApplication(sys.argv)

# The QWidget widget is the base class of all user interface objects in PyQt4.
w = QMainWindow()

# Set window size.
w.resize(320, 240)

# Set window title
w.setWindowTitle("Hello World!")

# Create main menu
mainMenu = w.menuBar()
mainMenu.setNativeMenuBar(False)
fileMenu = mainMenu.addMenu('File')
dataMaintMenu = mainMenu.addMenu('Data Maintenance')
reportingMenu = mainMenu.addMenu('Reporting')
aboutMenu = mainMenu.addMenu('About')

# Create Add menu 
add=QAction("Add new record")
dataMaintMenu.addAction(add)
dataMaintMenu.triggered[QAction].connect(self.openAddWindow)
self.setLayout(layout)

# Create Edit menu
edit=QAction("Edit new record")
dataMaintMenu.addAction(edit)
dataMaintMenu.triggered[QAction].connect(self.openEditWindow)
self.setLayout(layout)

# Create Delete menu
delete=QAction("Delete new record")
dataMaintMenu.addAction(delete)
dataMaintMenu.triggered[QAction].connect(self.openDeleteWindow)
self.setLayout(layout)

# Add exit button
exitButton = QAction(QIcon('exit24.png'), 'Exit', w)
exitButton.setShortcut('Ctrl+Q')
exitButton.setStatusTip('Exit application')
exitButton.triggered.connect(w.close)
fileMenu.addAction(exitButton)

def openAddWindow(self):
    self.window = QtWidgets.QMainWindow()
    self.ui = Ui_AddWindow()
    self.ui.setupUi(self.window)
    MainWindow.hide()
    self.window.show()

def openEditWindow(self):
    self.window = QtWidgets.QMainWindow()
    self.ui = Ui_EditWindow()
    self.ui.setupUi(self.window)
    MainWindow.hide()
    self.window.show()

def openDeleteWindow(self):
    self.window = QtWidgets.QMainWindow()
    self.ui = Ui_DeleteWindow()
    self.ui.setupUi(self.window)
    MainWindow.hide()
    self.window.show()

# Show window
w.show()

sys.exit(a.exec_())

而且我也只包含了我的Add.py文件,没有删除或编辑

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_AddWindow(object):
    def setupUi(self, AddWindow):
        AddWindow.setObjectName(_fromUtf8("AddWindow"))
        AddWindow.resize(653, 424)
        self.centralwidget = QtGui.QWidget(AddWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(50, 30, 301, 31))
        self.label.setObjectName(_fromUtf8("label"))
        self.label_2 = QtGui.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(50, 80, 101, 16))
        self.label_2.setObjectName(_fromUtf8("label_2"))
        self.label_3 = QtGui.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(50, 120, 181, 16))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.label_4 = QtGui.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(50, 170, 171, 16))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.label_5 = QtGui.QLabel(self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(50, 210, 211, 16))
        self.label_5.setObjectName(_fromUtf8("label_5"))
        self.label_6 = QtGui.QLabel(self.centralwidget)
        self.label_6.setGeometry(QtCore.QRect(50, 240, 161, 16))
        self.label_6.setObjectName(_fromUtf8("label_6"))
        self.label_7 = QtGui.QLabel(self.centralwidget)
        self.label_7.setGeometry(QtCore.QRect(50, 270, 161, 16))
        self.label_7.setObjectName(_fromUtf8("label_7"))
        self.lineEdit = QtGui.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(260, 79, 331, 21))
        self.lineEdit.setObjectName(_fromUtf8("lineEdit"))
        self.lineEdit_2 = QtGui.QLineEdit(self.centralwidget)
        self.lineEdit_2.setGeometry(QtCore.QRect(260, 120, 201, 20))
        self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2"))
        self.lineEdit_3 = QtGui.QLineEdit(self.centralwidget)
        self.lineEdit_3.setGeometry(QtCore.QRect(260, 170, 191, 20))
        self.lineEdit_3.setObjectName(_fromUtf8("lineEdit_3"))
        self.lineEdit_4 = QtGui.QLineEdit(self.centralwidget)
        self.lineEdit_4.setGeometry(QtCore.QRect(260, 240, 191, 20))
        self.lineEdit_4.setObjectName(_fromUtf8("lineEdit_4"))
        self.comboBox = QtGui.QComboBox(self.centralwidget)
        self.comboBox.setGeometry(QtCore.QRect(260, 210, 361, 22))
        self.comboBox.setObjectName(_fromUtf8("comboBox"))
        self.comboBox_2 = QtGui.QComboBox(self.centralwidget)
        self.comboBox_2.setGeometry(QtCore.QRect(260, 270, 301, 22))
        self.comboBox_2.setObjectName(_fromUtf8("comboBox_2"))
        self.pushButton = QtGui.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(160, 300, 231, 71))
        self.pushButton.setObjectName(_fromUtf8("pushButton"))
        AddWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(AddWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 653, 21))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        AddWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(AddWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        AddWindow.setStatusBar(self.statusbar)

        self.retranslateUi(AddWindow)
        QtCore.QMetaObject.connectSlotsByName(AddWindow)

    def retranslateUi(self, AddWindow):
        AddWindow.setWindowTitle(_translate("AddWindow", "MainWindow", None))
        self.label.setText(_translate("AddWindow", "<html><head/><body><p><span style=\" font-size:12pt; font-weight:600;\">Register your details</span></p></body></html>", None))
        self.label_2.setText(_translate("AddWindow", "Name and Surname", None))
        self.label_3.setText(_translate("AddWindow", "Unisa Student number (if applicable)", None))
        self.label_4.setText(_translate("AddWindow", "RSA Identity Number", None))
        self.label_5.setText(_translate("AddWindow", "Community Project you are interested In", None))
        self.label_6.setText(_translate("AddWindow", "Contact Cell or Tel Number", None))
        self.label_7.setText(_translate("AddWindow", "Nearest Unisa Regional Office", None))
        self.pushButton.setText(_translate("AddWindow", "DONE", None))

1 个答案:

答案 0 :(得分:0)

看这行:

add=QAction("Add new record")

正如您的错误所述,QAction没有仅接受一个字符串的构造函数。添加parent参数(我建议使用dataMaintMenuNone作为父元素):

add = QAction("Add new record", dataMaintMenu)