0

I am new to programming. The following is the design i made in Qt Designer and converted the ui file to py using pyuic5 -x file.ui -o file.py command. I am trying to minimize the program using a custom pushbutton. I tried all sorts of methods but it does not work. Any help is greatly appreciated.

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'home.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow


class Ui_Dialog(QMainWindow):
    def __init__(self):
        super().__init__()
        self.pushButton_4 = None
        self.pushButton_3 = None
        self.pushButton_2 = None
        self.pushButton = None
        self.frame = None

    def setupUi(self, home_ui):
        home_ui.setObjectName("Dialog")
        self.frame = QtWidgets.QFrame(home_ui)
        self.frame.setEnabled(True)
        self.frame.setGeometry(QtCore.QRect(10, 10, 661, 500))
        self.frame.setStyleSheet("background-color: rgb(0, 0, 0);\n"
                                 "border-top-left-radius: 100px;\n"
                                 "border-bottom-right-radius: 100px;\n"
                                 "")
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.pushButton = QtWidgets.QPushButton(self.frame)
        self.pushButton.setGeometry(QtCore.QRect(240, 120, 191, 101))
        font = QtGui.QFont()
        font.setFamily("Book Antiqua")
        font.setPointSize(-1)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.pushButton.setFont(font)
        self.pushButton.setStyleSheet("QPushButton#pushButton{\n"
                                      "background-color: qlineargradient(spread:reflect, x1:0.5, y1:1, x2:0.5, y2:0, "
                                      "stop:0.5 rgba(0, 107, 4, 255), stop:1 rgba(0, 255, 0, 255));\n "
                                      "border-radius: 50px;\n"
                                      "border-style: solid;\n"
                                      "border-width: 10px;\n"
                                      "border-color: rgb(29, 29, 29);\n"
                                      "color: white;\n"
                                      "font: bold;\n"
                                      "font-size: 20px;\n"
                                      "font-family: Book Antiqua;\n"
                                      "}\n"
                                      "QPushButton#pushButton:hover{\n"
                                      "background-color: qlineargradient(spread:reflect, x1:0.5, y1:1, x2:0.5, y2:0, "
                                      "stop:0.17757 rgba(0, 255, 9, 255), stop:0.570093 rgba(124, 255, 136, 255));\n "
                                      "border-radius: 50px;\n"
                                      "border-style: solid;\n"
                                      "border-width: 10px;\n"
                                      "border-color: rgb(29, 29, 29);\n"
                                      "color: black;\n"
                                      "font: bold;\n"
                                      "font-size: 20px;\n"
                                      "font-family: Book Antiqua;\n"
                                      "}\n"
                                      "QPushButton#pushButton:pressed{\n"
                                      "padding-left: 5px;\n"
                                      "padding-top: 5px;\n"
                                      "background-color:rgba(105, 118, 132, 200);\n"
                                      "}")
        self.pushButton.setCheckable(False)
        self.pushButton.setDefault(False)
        self.pushButton.setFlat(False)
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.frame)
        self.pushButton_2.setGeometry(QtCore.QRect(240, 250, 191, 101))
        self.pushButton_2.setStyleSheet("QPushButton#pushButton_2{\n"
                                        "background-color: qlineargradient(spread:reflect, x1:0.5, y1:1, x2:0.5, "
                                        "y2:0, stop:0.5 rgba(0, 107, 4, 255), stop:1 rgba(0, 255, 0, 255));\n "
                                        "border-radius: 50px;\n"
                                        "border-style: solid;\n"
                                        "border-width: 10px;\n"
                                        "border-color: rgb(29, 29, 29);\n"
                                        "color: white;\n"
                                        "font: bold;\n"
                                        "font-size: 20px;\n"
                                        "font-family: Book Antiqua;\n"
                                        "}\n"
                                        "QPushButton#pushButton_2:hover{\n"
                                        "background-color: qlineargradient(spread:reflect, x1:0.5, y1:1, x2:0.5, "
                                        "y2:0, stop:0.17757 rgba(0, 255, 9, 255), stop:0.570093 rgba(124, 255, 136, "
                                        "255));\n "
                                        "border-radius: 50px;\n"
                                        "border-style: solid;\n"
                                        "border-width: 10px;\n"
                                        "border-color: rgb(29, 29, 29);\n"
                                        "color: black;\n"
                                        "font: bold;\n"
                                        "font-size: 20px;\n"
                                        "font-family: Book Antiqua;\n"
                                        "}\n"
                                        "QPushButton#pushButton_2:pressed{\n"
                                        "padding-left: 5px;\n"
                                        "padding-top: 5px;\n"
                                        "background-color:rgba(105, 118, 132, 200);\n"
                                        "}")
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(self.frame)
        self.pushButton_3.clicked.connect(QApplication.instance().quit)
        self.pushButton_3.setGeometry(QtCore.QRect(620, 0, 41, 21))
        self.pushButton_3.setStyleSheet("QPushButton#pushButton_3{\n"
                                        "background-color: qlineargradient(spread:pad, x1:0.5, y1:1, x2:0.5, y2:0, "
                                        "stop:0.397196 rgba(119, 0, 0, 255), stop:1 rgba(255, 0, 0, 255));\n "
                                        "border-style: solid;\n"
                                        "border-width: 1px;\n"
                                        "border-color: rgb(29, 29, 29);\n"
                                        "color: black;\n"
                                        "font: bold;\n"
                                        "font-size: 20px;\n"
                                        "}\n"
                                        "QPushButton#pushButton_3:hover{\n"
                                        "background-color: qlineargradient(spread:pad, x1:0.5, y1:1, x2:0.5, y2:0, "
                                        "stop:0.504673 rgba(223, 0, 0, 255), stop:1 rgba(255, 48, 48, 255));\n "
                                        "border-style: solid;\n"
                                        "border-width: 1px;\n"
                                        "border-color: rgb(29, 29, 29);\n"
                                        "color: black;\n"
                                        "font: bold;\n"
                                        "font-size: 20px;\n"
                                        "}\n"
                                        "QPushButton#pushButton_3:pressed{\n"
                                        "padding-top: 1px;\n"
                                        "padding-left: 1px;\n"
                                        "}")
        self.pushButton_3.setCheckable(False)
        self.pushButton_3.setDefault(False)
        self.pushButton_3.setFlat(False)
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_4 = QtWidgets.QPushButton(self.frame)
        self.pushButton_4.clicked.connect(self.minimize)
        self.pushButton_4.setGeometry(QtCore.QRect(580, 0, 41, 21))
        self.pushButton_4.setStyleSheet("QPushButton#pushButton_4{\n"
                                        "background-color: qlineargradient(spread:pad, x1:0.5, y1:1, x2:0.5, y2:0, "
                                        "stop:0.5 rgba(0, 0, 141, 255), stop:1 rgba(0, 0, 255, 255));\n "
                                        "border-style: solid;\n"
                                        "border-width: 1px;\n"
                                        "border-color: rgb(29, 29, 29);\n"
                                        "color: black;\n"
                                        "font: bold;\n"
                                        "font-size: 25px;\n"
                                        "}\n"
                                        "QPushButton#pushButton_4:hover{\n"
                                        "background-color: qlineargradient(spread:pad, x1:0.5, y1:1, x2:0.5, y2:0, "
                                        "stop:0 rgba(0, 114, 255, 255), stop:1 rgba(0, 255, 255, 255));\n "
                                        "border-style: solid;\n"
                                        "border-width: 1px;\n"
                                        "border-color: rgb(29, 29, 29);\n"
                                        "color: black;\n"
                                        "font: bold;\n"
                                        "font-size: 25px;\n"
                                        "}\n"
                                        "QPushButton#pushButton_4:pressed{\n"
                                        "padding-top: 2px;\n"
                                        "}")
        self.pushButton_4.setCheckable(False)
        self.pushButton_4.setDefault(False)
        self.pushButton_4.setFlat(False)
        self.pushButton_4.setObjectName("pushButton_4")

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

    def retranslateUi(self, home_ui):
        _translate = QtCore.QCoreApplication.translate
        home_ui.setAttribute(Qt.WA_TranslucentBackground)
        home_ui.setWindowOpacity(0.6)
        home_ui.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.pushButton.setText(_translate("Dialog", "OPEN FILE"))
        self.pushButton_2.setText(_translate("Dialog", "SEARCH"))
        self.pushButton_3.setText(_translate("Dialog", "X"))
        self.pushButton_4.setText(_translate("Dialog", "-"))

    def minimize(self):
        self.showMinimized()


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

As you can see I have created a minimize function and called in the button. But nothing happens. While replacing the button code with

self.pushButton_4.clicked.connect(QMainWindow.showMinimized)

which i came up myself, resulted in an error and Process finished with exit code -1073740791 (0xC0000409).

Which is to be expected.:-)

Thankyou for your valuable time.

CyberNoob
  • 37
  • 7
  • The specific problem is that you're calling `minimize` on the wrong instance (the `self.minimize` refers to `ui`, not `Dialog`). The *actual* problem is that you're editing a `pyuic` generated file, which is considered a bad practice for many reasons, and in this specific case it's because it creates confusion about how those classes work. Please follow the official guidelines about [using Designer](https://www.riverbankcomputing.com/static/Docs/PyQt5/designer.html) to learn how to *properly* use those files, and then implement your function on an appropriate subclass. – musicamante Jan 01 '22 at 18:03

1 Answers1

1

I just implement maximize button to your code, but seems your mainWindow cannot be maximized:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow

class Ui_Dialog(QMainWindow):
    def __init__(self):
        super().__init__()
        self.pushButton_4 = None
        self.pushButton_3 = None
        self.pushButton_2 = None
        self.pushButton = None
        self.frame = None


    def setupUi(self, home_ui):
        home_ui.setObjectName("Dialog")
        self.frame = QtWidgets.QFrame(home_ui)
        self.frame.setEnabled(True)
        self.frame.setGeometry(QtCore.QRect(10, 10, 661, 500))
        self.frame.setStyleSheet(
            "background-color: rgb(0, 0, 0);\n"
            "border-top-left-radius: 100px;\n"
            "border-bottom-right-radius: 100px;\n"
            ""
        )
        self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.pushButton = QtWidgets.QPushButton(self.frame)
        self.pushButton.setGeometry(QtCore.QRect(240, 120, 191, 101))
        font = QtGui.QFont()
        font.setFamily("Book Antiqua")
        font.setPointSize(-1)
        font.setBold(True)
        font.setItalic(False)
        font.setWeight(75)
        self.pushButton.setFont(font)
        self.pushButton.setStyleSheet(
            "QPushButton#pushButton{\n"
            "background-color: qlineargradient(spread:reflect, x1:0.5, y1:1, x2:0.5, y2:0, "
            "stop:0.5 rgba(0, 107, 4, 255), stop:1 rgba(0, 255, 0, 255));\n "
            "border-radius: 50px;\n"
            "border-style: solid;\n"
            "border-width: 10px;\n"
            "border-color: rgb(29, 29, 29);\n"
            "color: white;\n"
            "font: bold;\n"
            "font-size: 20px;\n"
            "font-family: Book Antiqua;\n"
            "}\n"
            "QPushButton#pushButton:hover{\n"
            "background-color: qlineargradient(spread:reflect, x1:0.5, y1:1, x2:0.5, y2:0, "
            "stop:0.17757 rgba(0, 255, 9, 255), stop:0.570093 rgba(124, 255, 136, 255));\n "
            "border-radius: 50px;\n"
            "border-style: solid;\n"
            "border-width: 10px;\n"
            "border-color: rgb(29, 29, 29);\n"
            "color: black;\n"
            "font: bold;\n"
            "font-size: 20px;\n"
            "font-family: Book Antiqua;\n"
            "}\n"
            "QPushButton#pushButton:pressed{\n"
            "padding-left: 5px;\n"
            "padding-top: 5px;\n"
            "background-color:rgba(105, 118, 132, 200);\n"
            "}"
        )
        self.pushButton.setCheckable(False)
        self.pushButton.setDefault(False)
        self.pushButton.setFlat(False)
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.frame)
        self.pushButton_2.setGeometry(QtCore.QRect(240, 250, 191, 101))
        self.pushButton_2.setStyleSheet(
            "QPushButton#pushButton_2{\n"
            "background-color: qlineargradient(spread:reflect, x1:0.5, y1:1, x2:0.5, "
            "y2:0, stop:0.5 rgba(0, 107, 4, 255), stop:1 rgba(0, 255, 0, 255));\n "
            "border-radius: 50px;\n"
            "border-style: solid;\n"
            "border-width: 10px;\n"
            "border-color: rgb(29, 29, 29);\n"
            "color: white;\n"
            "font: bold;\n"
            "font-size: 20px;\n"
            "font-family: Book Antiqua;\n"
            "}\n"
            "QPushButton#pushButton_2:hover{\n"
            "background-color: qlineargradient(spread:reflect, x1:0.5, y1:1, x2:0.5, "
            "y2:0, stop:0.17757 rgba(0, 255, 9, 255), stop:0.570093 rgba(124, 255, 136, "
            "255));\n "
            "border-radius: 50px;\n"
            "border-style: solid;\n"
            "border-width: 10px;\n"
            "border-color: rgb(29, 29, 29);\n"
            "color: black;\n"
            "font: bold;\n"
            "font-size: 20px;\n"
            "font-family: Book Antiqua;\n"
            "}\n"
            "QPushButton#pushButton_2:pressed{\n"
            "padding-left: 5px;\n"
            "padding-top: 5px;\n"
            "background-color:rgba(105, 118, 132, 200);\n"
            "}"
        )
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_3 = QtWidgets.QPushButton(self.frame)
        self.pushButton_3.clicked.connect(QApplication.instance().quit)
        self.pushButton_3.setGeometry(QtCore.QRect(620, 0, 41, 21))
        self.pushButton_3.setStyleSheet(
            "QPushButton#pushButton_3{\n"
            "background-color: qlineargradient(spread:pad, x1:0.5, y1:1, x2:0.5, y2:0, "
            "stop:0.397196 rgba(119, 0, 0, 255), stop:1 rgba(255, 0, 0, 255));\n "
            "border-style: solid;\n"
            "border-width: 1px;\n"
            "border-color: rgb(29, 29, 29);\n"
            "color: black;\n"
            "font: bold;\n"
            "font-size: 20px;\n"
            "}\n"
            "QPushButton#pushButton_3:hover{\n"
            "background-color: qlineargradient(spread:pad, x1:0.5, y1:1, x2:0.5, y2:0, "
            "stop:0.504673 rgba(223, 0, 0, 255), stop:1 rgba(255, 48, 48, 255));\n "
            "border-style: solid;\n"
            "border-width: 1px;\n"
            "border-color: rgb(29, 29, 29);\n"
            "color: black;\n"
            "font: bold;\n"
            "font-size: 20px;\n"
            "}\n"
            "QPushButton#pushButton_3:pressed{\n"
            "padding-top: 1px;\n"
            "padding-left: 1px;\n"
            "}"
        )
        self.pushButton_3.setCheckable(False)
        self.pushButton_3.setDefault(False)
        self.pushButton_3.setFlat(False)
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_4 = QtWidgets.QPushButton(self.frame)
        self.pushButton_4.clicked.connect(self.minimize)
        self.pushButton_4.setGeometry(QtCore.QRect(540, 0, 41, 21))
        self.pushButton_4.setStyleSheet(
            "QPushButton#pushButton_4{\n"
            "background-color: qlineargradient(spread:pad, x1:0.5, y1:1, x2:0.5, y2:0, "
            "stop:0.5 rgba(0, 0, 141, 255), stop:1 rgba(0, 0, 255, 255));\n "
            "border-style: solid;\n"
            "border-width: 1px;\n"
            "border-color: rgb(29, 29, 29);\n"
            "color: black;\n"
            "font: bold;\n"
            "font-size: 25px;\n"
            "}\n"
            "QPushButton#pushButton_4:hover{\n"
            "background-color: qlineargradient(spread:pad, x1:0.5, y1:1, x2:0.5, y2:0, "
            "stop:0 rgba(0, 114, 255, 255), stop:1 rgba(0, 255, 255, 255));\n "
            "border-style: solid;\n"
            "border-width: 1px;\n"
            "border-color: rgb(29, 29, 29);\n"
            "color: black;\n"
            "font: bold;\n"
            "font-size: 25px;\n"
            "}\n"
            "QPushButton#pushButton_4:pressed{\n"
            "padding-top: 2px;\n"
            "}"
        )
        self.pushButton_4.setCheckable(False)
        self.pushButton_4.setDefault(False)
        self.pushButton_4.setFlat(False)
        self.pushButton_4.setObjectName("pushButton_4")
        self.pushButton_5 = QtWidgets.QPushButton(self.frame)
        self.pushButton_5.clicked.connect(self.maxer)
        self.pushButton_5.setGeometry(QtCore.QRect(580, 0, 41, 21))
        self.pushButton_5.setStyleSheet(
            "QPushButton#pushButton_5{\n"
            "background-color: qlineargradient(spread:pad, x1:0.5, y1:1, x2:0.5, y2:0, "
            "stop:0.5 rgba(0, 0, 141, 255), stop:1 rgba(0, 0, 255, 255));\n "
            "border-style: solid;\n"
            "border-width: 1px;\n"
            "border-color: rgb(29, 29, 29);\n"
            "color: black;\n"
            "font: bold;\n"
            "font-size: 25px;\n"
            "}\n"
            "QPushButton#pushButton_5:hover{\n"
            "background-color: qlineargradient(spread:pad, x1:0.5, y1:1, x2:0.5, y2:0, "
            "stop:0 rgba(0, 114, 255, 255), stop:1 rgba(0, 255, 255, 255));\n "
            "border-style: solid;\n"
            "border-width: 1px;\n"
            "border-color: rgb(29, 29, 29);\n"
            "color: black;\n"
            "font: bold;\n"
            "font-size: 25px;\n"
            "}\n"
            "QPushButton#pushButton_5:pressed{\n"
            "padding-top: 2px;\n"
            "}"
        )
        self.pushButton_5.setCheckable(False)
        self.pushButton_5.setDefault(False)
        self.pushButton_5.setFlat(False)
        self.pushButton_5.setObjectName("pushButton_5")

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

    def maxer(self):
        state = int(self.windowState())
        if state == 0:
            self.showMaximized()
        else:
            self.showNormal()

    def retranslateUi(self, home_ui):
        _translate = QtCore.QCoreApplication.translate
        home_ui.setAttribute(Qt.WA_TranslucentBackground)
        home_ui.setWindowOpacity(0.6)
        home_ui.setWindowFlag(QtCore.Qt.FramelessWindowHint)
        self.pushButton.setText(_translate("Dialog", "OPEN FILE"))
        self.pushButton_2.setText(_translate("Dialog", "SEARCH"))
        self.pushButton_3.setText(_translate("Dialog", "X"))
        self.pushButton_4.setText(_translate("Dialog", "-"))

    def minimize(self):
        self.showMinimized()

if name == "main": import sys

     app = QtWidgets.QApplication(sys.argv)
     Dialog = QtWidgets.QDialog()
     ui = Ui_Dialog()
     ui.setupUi(Dialog)
     Dialog.show()
     sys.exit(app.exec_())
Feras Alfrih
  • 492
  • 3
  • 11
  • 1
    This answer would be a lot better if it showed how the class should be used with the code in the question. An important issue here is that the OP (who's a newbie) does not know how to use the pyuic output correctly. – ekhumoro Jan 02 '22 at 00:33
  • @Feras Alfrih I copied the above code and created a new file to understand how this code works. But ` buttonLayout.addWidget(new)` in this line it says new dosen't have a reference. Thank You – CyberNoob Jan 02 '22 at 06:34
  • @CyberNoob, sorry about it, i thought i deleted all related fields to **new**. I've updated the answer – Feras Alfrih Jan 02 '22 at 12:09