Search code examples
pythonpyqtpyqt5qt-designer

Object has no attribute PyQt5 | Removing Maximize Button


I am trying to remove the maximize button in the main window of my program, the issue is I get an error that stats:

'main_window_logic' object has no attribute 'Main_Window'

I can reference any object in the main window, I just can not reference the main window itself I used Qt Designer to create the windows.

Any explanation to this? Is this caused from using uic.loadUiType()?

Python(PyQt5) Code:

from PyQt5 import QtCore, QtGui, QtWidgets, uic
import sys

baseUIClass, baseUIWidget = uic.loadUiType("main_window.ui")

class main_window_logic(baseUIWidget, baseUIClass):
    """ Handles Logic for the Main Window (main_window.ui)"""
    def __init__(self, parent = None):
        super(main_window_logic,self).__init__(parent)
        self.setupUi(self)
        self.listWidget.setAlternatingRowColors(True)
        self.Main_Window.setWindowFlags(QtCore.Qt.WindowCloseButtonHint | QtCore.Qt.WindowMinimizeButtonHint)
   

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    ui = main_window_logic(None)
    ui.show()
    sys.exit(app.exec_())

XML Code:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Main_Window</class>
 <widget class="QMainWindow" name="Main_Window">
  <property name="enabled">
   <bool>true</bool>
  </property>
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>930</width>
    <height>715</height>
   </rect>
  </property>
  <property name="maximumSize">
   <size>
    <width>930</width>
    <height>715</height>
   </size>
  </property>
  <property name="sizeIncrement">
   <size>
    <width>0</width>
    <height>4</height>
   </size>
  </property>
  <property name="baseSize">
   <size>
    <width>4</width>
    <height>0</height>
   </size>
  </property>
  <property name="cursor">
   <cursorShape>ArrowCursor</cursorShape>
  </property>
  <property name="windowTitle">
   <string>title</string>
  </property>
  <property name="windowOpacity">
   <double>4.000000000000000</double>
  </property>
  <property name="styleSheet">
   <string notr="true">QMainWindow {
background-color: qlineargradient(spread:pad, x1:0.0100548, y1:0.631, x2:0.229627, y2:0.931, stop:0.440607 rgba(97, 35, 86, 255), stop:1 rgba(105, 25, 98, 255));

}

    </string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QFrame" name="frame">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>20</y>
      <width>251</width>
      <height>661</height>
     </rect>
    </property>
    <property name="baseSize">
     <size>
      <width>0</width>
      <height>0</height>
     </size>
    </property>
    <property name="styleSheet">
     <string notr="true">QFrame {

    background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(87, 54, 185, 255), stop:1 rgba(168, 41, 41, 255));

}   </string>
    </property>
    <property name="frameShape">
     <enum>QFrame::StyledPanel</enum>
    </property>
    <property name="frameShadow">
     <enum>QFrame::Raised</enum>
    </property>
    <widget class="QListWidget" name="listWidget">
     <property name="geometry">
      <rect>
       <x>0</x>
       <y>0</y>
       <width>251</width>
       <height>661</height>
      </rect>
     </property>
     <property name="baseSize">
      <size>
       <width>0</width>
       <height>0</height>
      </size>
     </property>
     <property name="styleSheet">
      <string notr="true">QListWidget{
    

font: 75 11pt &quot;Ubuntu Mono&quot;;

    background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(87, 54, 185, 255), stop:1 rgba(168, 41, 41, 255));
;
    alternate-background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0 rgba(226, 24, 24, 255), stop:0.985377 rgba(207, 112, 0, 255));
    


}


</string>
     </property>
     <property name="itemAlignment">
      <set>Qt::AlignCenter</set>
     </property>
    </widget>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>930</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

Thank you.


Solution

  • The Main_Window is the toplevel, that is, the window, which in your case is accessed through self:

    self.setWindowFlags(
        QtCore.Qt.WindowCloseButtonHint | QtCore.Qt.WindowMinimizeButtonHint
    )