Мой вопрос похож на pyQt5 AttributeError: у объекта 'bool' нет атрибута 'txtCustCode' , но этот вопрос не был разрешен.
Моя ситуация: я создание формы, из которой администратор может вводить информацию о новом пользователе. Когда информация введена, администратор может нажать кнопку «отправить», чтобы отправить информацию в базу данных, или кнопку «отменить», чтобы очистить форму для запуска fre sh.
Пока что я был в состоянии построить форму и отобразить ее, как я хочу. При нажатии кнопки «отмена»; однако я получаю следующее сообщение об ошибке:
Исключение "необработанный AttributeError"
У объекта 'bool' нет атрибута 'layout_base'
Я делаю не понимаю, почему я не получаю это сообщение об ошибке, когда show_add_new_user_form вызывается из
self.button_add_user.clicked.connect (self.show_add_new_user_form)
но я получаю сообщение об ошибке, когда оно вызывается из
button_cancel.clicked.connect (self.on_button_cancel_clicked)
или
button_cancel.clicked.connect (AdminViewTab.show_add_new_user_form)
Мои файлы: main.py
import sys
from PyQt5.QtWidgets import QApplication
from main_view import MainWindow
class Main(QApplication):
""" Record keeping application for Vigo Twp Fire Dept. """
def __init__(self, sys_argv):
""" Initializer. """
super(Main, self).__init__(sys_argv)
self.main_view = MainWindow()
self.main_view.show()
if __name__ == '__main__':
vigo_main = Main(sys.argv)
sys.exit(vigo_main.exec_())
main_view.py
from PyQt5.QtWidgets import (QApplication,
QMainWindow,
QTabWidget,
QWidget,
QLabel,
QHBoxLayout)
from PyQt5.QtCore import (pyqtSlot)
from admin_view import AdminViewTab
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow,self).__init__()
tabs = QTabWidget()
tabs.setDocumentMode(True)
tabs.setTabPosition(QTabWidget.North)
tabs.setMovable(False)
self.setWindowTitle("Vigo Township Record Keeping")
self.tab_user_view = QWidget()
self.tab_admin_view = AdminViewTab()
tabs.addTab(self.tab_user_view, 'User Functions')
tabs.addTab(self.tab_admin_view, 'Administrator Functions')
self.user_func_view()
self.setCentralWidget(tabs)
# Menu
self.menu = self.menuBar()
self.file_menu = self.menu.addMenu("File")
@pyqtSlot()
def exit_app(self):
QApplication.quit()
def user_func_view(self):
layout = QHBoxLayout()
label = QLabel("User View Page")
layout.addWidget(label)
self.tab_user_view.setLayout(layout)
admin_view .py
from PyQt5.QtWidgets import (QFormLayout,
QHBoxLayout,
QLabel,
QLineEdit,
QPushButton,
QVBoxLayout,
QWidget)
from PyQt5.QtCore import (pyqtSlot)
class AdminViewTab(QWidget):
def __init__(self, parent=None):
super(AdminViewTab, self).__init__(parent=parent)
# Define the base layout
self.layout_base = QHBoxLayout()
# Define the layout for the buttons.
layout_buttons = QVBoxLayout()
widget_buttons = QWidget()
# Following the principle above, I will create a blank widget.
# This blank widget will then be populated with the forms needed to
# complete the admin functions
widget_work_area = QWidget()
# Define the buttons for the administrator functions
self.button_add_user = QPushButton('Add New Personnel', self)
self.button_update_user = QPushButton('Update Personnel Information')
self.button_list_roster = QPushButton('List Current Roster')
# Define the signals for the administrator buttons
self.button_add_user.clicked.connect(self.show_add_new_user_form)
# Add the buttons to the buttons layout
layout_buttons.addWidget(self.button_add_user)
layout_buttons.addWidget(self.button_update_user)
layout_buttons.addWidget(self.button_list_roster)
# add the buttons layout to this widget, for some reason.
widget_buttons.setLayout(layout_buttons)
# Add the widget with the buttons to the base layout
self.layout_base.addWidget(widget_buttons)
# Add the work area Widget to the base layout
self.layout_base.addWidget(widget_work_area)
self.setLayout(self.layout_base)
@pyqtSlot()
def show_add_new_user_form(self):
# print("This will display the new user form")
# Remove any widgets that might be in the
myWidget = self.layout_base.itemAt(1).widget()
myWidget.setParent(None)
self.layout_base.removeWidget(myWidget)
self.layout_base.addWidget(AddNewUserForm())
class AddNewUserForm(QWidget):
def __init__(self, parent=None):
super(AddNewUserForm, self).__init__(parent=parent)
form_layout = QFormLayout()
form_layout.addRow(QLabel("Personnel Information"))
form_layout.addRow(QLabel('* Denotes Required Field'))
call_sign = QLineEdit()
form_layout.addRow("Call Sign", call_sign)
button_submit = QPushButton("Submit")
button_cancel = QPushButton("Cancel")
form_layout.addRow(button_submit, button_cancel)
# Connect submit button to submit action in controller
button_submit.clicked.connect(self.on_button_submit_clicked)
# Cancel button will clear the form and display a blank form
# This is essentially the same as when the "add new user" button
# is pressed initially.
# button_cancel.clicked.connect(self.on_button_cancel_clicked)
button_cancel.clicked.connect(AdminViewTab.show_add_new_user_form)
self.setLayout(form_layout)
@pyqtSlot()
def on_button_submit_clicked(self):
print("Information Sent to Database")
@pyqtSlot()
def on_button_cancel_clicked(self):
print("Form cleared and reset")
AdminViewTab.show_add_new_user_form()
Спасибо, что нашли время помочь мне с этой проблемой.