AttributeError: 'Ui_MainWindow' object has no attribute 'frame_left_menu'[SOLVED]
-
# Form implementation generated from reading ui file 'main.ui' # # Created by: PyQt6 UI code generator 6.3.1 # # WARNING: Any manual changes made to this file will be lost when pyuic6 is # run again. Do not edit this file unless you know what you are doing. from PyQt6 import QtCore, QtGui, QtWidgets from PyQt6.QtCore import QPropertyAnimation class Ui_MainWindow(object): def __init__(self): super().__init__() self.animation = None def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(1000, 600) MainWindow.setMinimumSize(QtCore.QSize(1000, 500)) MainWindow.setStyleSheet("background-color: rgb(45, 45, 45);") self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget) self.verticalLayout.setContentsMargins(0, 0, 0, 0) self.verticalLayout.setSpacing(0) self.verticalLayout.setObjectName("verticalLayout") self.top_bar = QtWidgets.QFrame(self.centralwidget) self.top_bar.setMaximumSize(QtCore.QSize(16777215, 40)) self.top_bar.setStyleSheet("background-color: rgb(35, 35, 35);") self.top_bar.setFrameShape(QtWidgets.QFrame.Shape.NoFrame) self.top_bar.setFrameShadow(QtWidgets.QFrame.Shadow.Raised) self.top_bar.setObjectName("top_bar") self.horizontalLayout = QtWidgets.QHBoxLayout(self.top_bar) self.horizontalLayout.setContentsMargins(0, 0, 0, 0) self.horizontalLayout.setSpacing(0) self.horizontalLayout.setObjectName("horizontalLayout") self.toggle_frame = QtWidgets.QFrame(self.top_bar) self.toggle_frame.setMaximumSize(QtCore.QSize(70, 40)) self.toggle_frame.setStyleSheet("background-color: rgb(62, 187, 255);") self.toggle_frame.setFrameShape(QtWidgets.QFrame.Shape.NoFrame) self.toggle_frame.setFrameShadow(QtWidgets.QFrame.Shadow.Raised) self.toggle_frame.setObjectName("toggle_frame") self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.toggle_frame) self.verticalLayout_2.setContentsMargins(0, 0, 0, 0) self.verticalLayout_2.setSpacing(0) self.verticalLayout_2.setObjectName("verticalLayout_2") self.btn_toggle = QtWidgets.QPushButton(self.toggle_frame) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Expanding) sizePolicy.setHorizontalStretch(0) sizePolicy.setVerticalStretch(0) sizePolicy.setHeightForWidth(self.btn_toggle.sizePolicy().hasHeightForWidth()) self.btn_toggle.setSizePolicy(sizePolicy) self.btn_toggle.setStyleSheet("QPushbutton{\n" " color: rgb(255, 255, 255);\n" " border: 0px solid;\n" "}\n" "\n" "QPushButton:hover{\n" " \n" " background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0.744, y2:0.869818, stop:0 rgba(0, 111, 255, 255), stop:1 rgba(102, 255, 176, 255));\n" "}") self.btn_toggle.setObjectName("btn_toggle") self.btn_toggle.clicked.connect(self.toggle_clicked(140, True)) self.verticalLayout_2.addWidget(self.btn_toggle) self.horizontalLayout.addWidget(self.toggle_frame) self.top_frame = QtWidgets.QFrame(self.top_bar) self.top_frame.setMaximumSize(QtCore.QSize(16777215, 40)) self.top_frame.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel) self.top_frame.setFrameShadow(QtWidgets.QFrame.Shadow.Raised) self.top_frame.setObjectName("top_frame") self.horizontalLayout.addWidget(self.top_frame) self.verticalLayout.addWidget(self.top_bar) self.content = QtWidgets.QFrame(self.centralwidget) self.content.setFrameShape(QtWidgets.QFrame.Shape.NoFrame) self.content.setFrameShadow(QtWidgets.QFrame.Shadow.Raised) self.content.setObjectName("content") self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.content) self.horizontalLayout_2.setContentsMargins(0, 0, 0, 0) self.horizontalLayout_2.setSpacing(0) self.horizontalLayout_2.setObjectName("horizontalLayout_2") self.frame_left_menu = QtWidgets.QFrame(self.content) self.frame_left_menu.setMinimumSize(QtCore.QSize(70, 0)) self.frame_left_menu.setMaximumSize(QtCore.QSize(70, 16777215)) self.frame_left_menu.setStyleSheet("background-color: rgb(35, 35, 35);") self.frame_left_menu.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel) self.frame_left_menu.setFrameShadow(QtWidgets.QFrame.Shadow.Raised) self.frame_left_menu.setObjectName("frame_left_menu") self.verticalLayout_3 = QtWidgets.QVBoxLayout(self.frame_left_menu) self.verticalLayout_3.setContentsMargins(0, 0, 0, 0) self.verticalLayout_3.setSpacing(0) self.verticalLayout_3.setObjectName("verticalLayout_3") self.frame_left_buttons = QtWidgets.QFrame(self.frame_left_menu) self.frame_left_buttons.setFrameShadow(QtWidgets.QFrame.Shadow.Raised) self.frame_left_buttons.setObjectName("frame_left_buttons") self.verticalLayout_4 = QtWidgets.QVBoxLayout(self.frame_left_buttons) self.verticalLayout_4.setContentsMargins(0, 0, 0, 0) self.verticalLayout_4.setSpacing(0) self.verticalLayout_4.setObjectName("verticalLayout_4") self.pushButton_2 = QtWidgets.QPushButton(self.frame_left_buttons) self.pushButton_2.setMinimumSize(QtCore.QSize(0, 40)) self.pushButton_2.setStyleSheet("QPushButton{\n" " \n" " background-color: rgb(45, 45, 45);\n" " color: rgb(255, 255, 255);\n" " border: 1px rgb(255, 255, 255) solid;\n" "}\n" "\n" "QPushButton:hover{\n" " \n" " background-color: rgb(85, 175, 255);\n" "}") self.pushButton_2.setObjectName("pushButton_2") self.verticalLayout_4.addWidget(self.pushButton_2) self.pushButton_3 = QtWidgets.QPushButton(self.frame_left_buttons) self.pushButton_3.setMinimumSize(QtCore.QSize(0, 40)) self.pushButton_3.setStyleSheet("QPushButton{\n" " \n" " background-color: rgb(45, 45, 45);\n" " color: rgb(255, 255, 255);\n" " border: 1px rgb(255, 255, 255) solid;\n" "}\n" "\n" "QPushButton:hover{\n" " \n" " background-color: rgb(85, 175, 255);\n" "}") self.pushButton_3.setObjectName("pushButton_3") self.verticalLayout_4.addWidget(self.pushButton_3) self.pushButton = QtWidgets.QPushButton(self.frame_left_buttons) self.pushButton.setMinimumSize(QtCore.QSize(0, 40)) self.pushButton.setStyleSheet("QPushButton{\n" " \n" " background-color: rgb(45, 45, 45);\n" " color: rgb(255, 255, 255);\n" " border: 1px rgb(255, 255, 255) solid;\n" "}\n" "\n" "QPushButton:hover{\n" " \n" " background-color: rgb(85, 175, 255);\n" "}") self.pushButton.setObjectName("pushButton") self.verticalLayout_4.addWidget(self.pushButton) self.verticalLayout_3.addWidget(self.frame_left_buttons, 0, QtCore.Qt.AlignmentFlag.AlignTop) self.horizontalLayout_2.addWidget(self.frame_left_menu) self.frame_pages = QtWidgets.QFrame(self.content) self.frame_pages.setStyleSheet("") self.frame_pages.setFrameShape(QtWidgets.QFrame.Shape.StyledPanel) self.frame_pages.setFrameShadow(QtWidgets.QFrame.Shadow.Raised) self.frame_pages.setObjectName("frame_pages") self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.frame_pages) self.verticalLayout_5.setObjectName("verticalLayout_5") self.stackedWidget = QtWidgets.QStackedWidget(self.frame_pages) self.stackedWidget.setObjectName("stackedWidget") self.page = QtWidgets.QWidget() self.page.setObjectName("page") self.stackedWidget.addWidget(self.page) self.page_2 = QtWidgets.QWidget() self.page_2.setObjectName("page_2") self.stackedWidget.addWidget(self.page_2) self.verticalLayout_5.addWidget(self.stackedWidget) self.horizontalLayout_2.addWidget(self.frame_pages) self.verticalLayout.addWidget(self.content) MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.btn_toggle.setText(_translate("MainWindow", "Toggle")) self.pushButton_2.setText(_translate("MainWindow", "PushButton")) self.pushButton_3.setText(_translate("MainWindow", "PushButton")) self.pushButton.setText(_translate("MainWindow", "PushButton")) def toggle_clicked(self, maxWidth, enable): if enable: # GET WIDTH wid = self.frame_left_menu.width() maxExtend = maxWidth standard = 70 # Set Max Width if wid == standard: widthExtend = maxExtend else: widthExtend = standard # animation self.animation = QPropertyAnimation(self.frame_left_menu, b"minimumWidth") self.animation.setDuration(400) self.animation.setStartValue(wid) self.animation.endValue(widthExtend) self.animation.start() if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec())
within this code, my aim was to just do basic connectivity and event handling for the toggle button (btn_toggle), but the error mentioned in the title keeps popping up, even though (self.frame_left_menu) definitely exists.
i wish to obtain the width of the frame(frame_left_menu) when the toggle button is clicked.
Please enlighten me on what I am doing wrong and/or a workaround to get the same result
The code has been converted from .ui file to .py file via pyuic6 command and I have installed PyQt6 already.the exact error is :-
Traceback (most recent call last): File "D:\Python\Pyhton\PyQT\project\ui_main.py", line 216, in <module> ui.setupUi(MainWindow) File "D:\Python\Pyhton\PyQT\project\ui_main.py", line 73, in setupUi self.btn_toggle.clicked.connect(self.toggle_clicked(140, True)) File "D:\Python\Pyhton\PyQT\project\ui_main.py", line 192, in toggle_clicked wid = self.frame_left_menu.width() AttributeError: 'Ui_MainWindow' object has no attribute 'frame_left_menu'
-
Hi and welcome to devnet,
Your connect statement is wrong.
self.btn_toggle.clicked.connect(self.toggle_clicked(140, True))
What happens is that you connect the signal to the value returned by the call to toggle_clicked. Since you are calling it now, the rest of the widgets have not yet been created and thus the error you are getting.
-
No it won't.
Here is this line rewritten for clarity about the sequence of events:
result = self.toggle_clicked(140, True) self.btn_toggle.clicked.connect(result)
If you want to pass variables like that to the slot when clicking the button, either use a lambda as the connect target or an intermediate parameter less slot that will call
toggle_clicked
with the parameters you want. -
alright, i think i understand
but i do not quite understand what you mean by "use a lambda as the connect target"
from what i found online, it seems to be something of this sortself.btn_toggle.clicked.connect(lambda: self.toggle_clicked(140, True))
and if this is what i am supposed to do, it just in turn gives me this error when i click the toggle button:
Process finished with exit code -1073740791 (0xC0000409)
it just seems to be an error suppression
if i am understanding this completely wrong, please help me out
thanks and regards -
@Flare said in AttributeError: 'Ui_MainWindow' object has no attribute 'frame_left_menu':
self.btn_toggle.clicked.connect(lambda: self.toggle_clicked(140, True))
Should be
self.btn_toggle.clicked.connect(lambda self: self.toggle_clicked(140, True))
-
@jsulm
i still get the same errorProcess finished with exit code -1073740791 (0xC0000409)
is there perhaps something wrong within my function? i get all other input within the function just fine before reaching the
wid = self.frame_left_menu.width()
line
thanks and regards -
What does the debugger tell you when you hit that line ?
-