我设计了一个Qt界面,该界面由主窗口和一个下拉菜单 File 组成,菜单中有一个名为 Add account 的项目。 >。当单击该项目时,将出现一个 Dialog 窗口,该窗口中有一个名为“添加电子邮件”的按钮,现在该按钮应该调用另一个对话框,以供用户输入其电子邮件详细信息。所有这些窗口/对话框都是使用Qt Designer设计的,因此每个窗口/对话框都包含在单独的.py文件中。有一个名为“ main.py
”的“中央”文件可以控制窗口/对话框,因此我使用python3 main.py
运行该程序。
问题陈述:单击文件菜单中的相应项目后,出现添加帐户对话框,但是当我出现以下错误时按下按钮添加电子邮件:
$ python3 main.py
Traceback (most recent call last):
File "main.py", line 32, in <module>
main()
File "main.py", line 27, in main
form.actionAdd_email.triggered.connect(open_email_dialog)
AttributeError: 'ExampleApp' object has no attribute 'actionAdd_email'
第32行只是main.py
中main方法的调用:
if __name__ == '__main__':
main()
第27行是
form.actionAdd_email.triggered.connect(open_email_dialog)
(另请参见下面的main
方法)。
尽管有这样一个事实,即以我对添加帐户项的方式添加了一种操作来调用此电子邮件输入对话框。
我用于调用添加帐户对话框的main.py
方法
与我用来调用添加电子邮件对话框的main.py
方法非常相似。
添加电子邮件按钮在文件addaccounts.py
中进行了描述:
self.addEmail = QtWidgets.QPushButton(addAccts)
self.addEmail.setGeometry(QtCore.QRect(360, 30, 113, 32))
self.addEmail.setObjectName("addEmail")
操作actionAdd_email
在同一文件中描述如下:
self.actionAdd_email = QtWidgets.QAction(addAccts)
self.actionAdd_email.setObjectName("actionAdd_email")
self.addEmail.addAction(self.actionAdd_email)
与从主窗口调用的actionAdd_accounts
完全相似。
我已经为此苦苦挣扎了很多时间,但是找不到问题所在。有人可以帮我吗?
请在下面找到完整的可验证示例的代码:
(“中央”模块)main.py
:
import sys, design, addemail, addaccounts
from PyQt5 import QtCore, QtGui, QtWidgets
class ExampleApp(QtWidgets.QMainWindow, design.Ui_MainWindow, addaccounts.Ui_addAccts, addemail.Ui_addEmail):
def __init__(self):
super(self.__class__, self).__init__()
self.setupUi(self)
def open_accts_dialog():
dialog = QtWidgets.QDialog()
dialog.ui = addaccounts.Ui_addAccts()
dialog.ui.setupUi(dialog)
dialog.exec_()
dialog.show()
def open_email_dialog():
dialog = QtWidgets.QDialog()
dialog.ui = addemail.Ui_addEmail()
dialog.ui.setupUi(dialog)
dialog.exec_()
dialog.show()
def main():
app = QtWidgets.QApplication(sys.argv)
form = ExampleApp()
form.actionAdd_accounts.triggered.connect(open_accts_dialog)
form.actionAdd_email.triggered.connect(open_email_dialog)
form.show()
app.exec_()
if __name__ == '__main__':
main()
(Qt主窗口)design.py
:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(552, 259)
MainWindow.setUnifiedTitleAndToolBarOnMac(False)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
font = QtGui.QFont()
font.setStrikeOut(False)
self.listWidget.setFont(font)
self.listWidget.setObjectName("listWidget")
self.verticalLayout.addWidget(self.listWidget)
MainWindow.setCentralWidget(self.centralwidget)
self.menuBar = QtWidgets.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 552, 22))
self.menuBar.setFocusPolicy(QtCore.Qt.ClickFocus)
self.menuBar.setAutoFillBackground(True)
self.menuBar.setDefaultUp(False)
self.menuBar.setNativeMenuBar(False)
self.menuBar.setObjectName("menuBar")
self.menuFile = QtWidgets.QMenu(self.menuBar)
self.menuFile.setObjectName("menuFile")
self.menuView = QtWidgets.QMenu(self.menuBar)
self.menuView.setObjectName("menuView")
self.menuHelp = QtWidgets.QMenu(self.menuBar)
self.menuHelp.setObjectName("menuHelp")
MainWindow.setMenuBar(self.menuBar)
self.actionAdd_accounts = QtWidgets.QAction(MainWindow)
self.actionAdd_accounts.setObjectName("actionAdd_accounts")
self.menuFile.addAction(self.actionAdd_accounts)
self.menuFile.addSeparator()
self.menuFile.addAction(self.actionExit)
self.menuBar.addAction(self.menuFile.menuAction())
self.retranslateUi(MainWindow)
self.actionStatus_bar.toggled['bool'].connect(self.statusbar.setVisible)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MyMess Aggregator"))
self.menuFile.setStatusTip(_translate("MainWindow", "File menu"))
self.menuFile.setTitle(_translate("MainWindow", "File"))
(Qt 添加帐户对话框)addaccounts.py
:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_addAccts(object):
def setupUi(self, addAccts):
addAccts.setObjectName("addAccts")
addAccts.resize(481, 299)
self.buttonBox = QtWidgets.QDialogButtonBox(addAccts)
self.buttonBox.setGeometry(QtCore.QRect(370, 160, 101, 331))
self.buttonBox.setOrientation(QtCore.Qt.Vertical)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Close)
self.buttonBox.setObjectName("buttonBox")
self.addEmail = QtWidgets.QPushButton(addAccts)
self.addEmail.setGeometry(QtCore.QRect(360, 30, 113, 32))
self.addEmail.setObjectName("addEmail")
self.listView = QtWidgets.QListView(addAccts)
self.listView.setGeometry(QtCore.QRect(10, 10, 341, 271))
self.listView.setObjectName("listView")
self.actionAdd_email = QtWidgets.QAction(addAccts)
self.actionAdd_email.setObjectName("actionAdd_email")
self.addEmail.addAction(self.actionAdd_email)
self.retranslateUi(addAccts)
self.buttonBox.accepted.connect(addAccts.accept)
QtCore.QMetaObject.connectSlotsByName(addAccts)
def retranslateUi(self, addAccts):
_translate = QtCore.QCoreApplication.translate
addAccts.setWindowTitle(_translate("addAccts", "Add accounts"))
self.addEmail.setText(_translate("addAccts", "Add Email"))
(添加电子邮件对话框)addemail.py
:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_addEmail(object):
def setupUi(self, addEmail):
addEmail.setObjectName("addEmail")
addEmail.resize(502, 213)
addEmail.setFocusPolicy(QtCore.Qt.StrongFocus)
addEmail.setAcceptDrops(False)
self.formLayoutWidget = QtWidgets.QWidget(addEmail)
self.formLayoutWidget.setGeometry(QtCore.QRect(0, 0, 501, 211))
self.formLayoutWidget.setObjectName("formLayoutWidget")
self.formLayout = QtWidgets.QFormLayout(self.formLayoutWidget)
self.formLayout.setLabelAlignment(QtCore.Qt.AlignJustify|QtCore.Qt.AlignTop)
self.formLayout.setFormAlignment(QtCore.Qt.AlignCenter)
self.formLayout.setContentsMargins(0, 0, 0, 0)
self.formLayout.setObjectName("formLayout")
self.label = QtWidgets.QLabel(self.formLayoutWidget)
self.label.setObjectName("label")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label)
self.lineEdit = QtWidgets.QLineEdit(self.formLayoutWidget)
self.lineEdit.setText("")
self.lineEdit.setObjectName("lineEdit")
self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.lineEdit)
self.label_2 = QtWidgets.QLabel(self.formLayoutWidget)
self.label_2.setObjectName("label_2")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.LabelRole, self.label_2)
self.emailPassField = QtWidgets.QLineEdit(self.formLayoutWidget)
self.emailPassField.setAutoFillBackground(False)
self.emailPassField.setInputMethodHints(QtCore.Qt.ImhHiddenText|QtCore.Qt.ImhNoAutoUppercase|QtCore.Qt.ImhNoPredictiveText|QtCore.Qt.ImhSensitiveData)
self.emailPassField.setInputMask("")
self.emailPassField.setEchoMode(QtWidgets.QLineEdit.Password)
self.emailPassField.setClearButtonEnabled(False)
self.emailPassField.setObjectName("emailPassField")
self.formLayout.setWidget(2, QtWidgets.QFormLayout.FieldRole, self.emailPassField)
self.buttonBox = QtWidgets.QDialogButtonBox(self.formLayoutWidget)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Close|QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.formLayout.setWidget(3, QtWidgets.QFormLayout.FieldRole, self.buttonBox)
self.comboBox = QtWidgets.QComboBox(self.formLayoutWidget)
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.comboBox)
self.label_3 = QtWidgets.QLabel(self.formLayoutWidget)
self.label_3.setObjectName("label_3")
self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label_3)
self.retranslateUi(addEmail)
self.buttonBox.accepted.connect(addEmail.accept)
self.buttonBox.rejected.connect(addEmail.reject)
QtCore.QMetaObject.connectSlotsByName(addEmail)
def retranslateUi(self, addEmail):
_translate = QtCore.QCoreApplication.translate
addEmail.setWindowTitle(_translate("addEmail", "Add Email account"))
self.label.setText(_translate("addEmail", "Email address:"))
self.label_2.setText(_translate("addEmail", "Password:"))
self.comboBox.setItemText(0, _translate("addEmail", "IMAP"))
self.comboBox.setItemText(1, _translate("addEmail", "POP3"))
self.label_3.setText(_translate("addEmail", "Email protocol:"))