PDA

View Full Version : How to add a custom minimize button in PyQt5?



AbhaySalvi
2nd January 2020, 09:54
I am new to PyQt5 and want to make an app with custom titlebar. So i removed it and placed three macos like buttons in which if a user clicks on the green button, it minimizes the window, but it just closes the window instead of minimizing it :( . It is giving me this error:

TypeError: min() takes 1 positional argument but 2 were given

Here is my code:


from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QWidget
from PyQt5.QtGui import QCursor, QWindow

class Ui_MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Wally")
self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

# background
self.label = QLabel("", self)
background = QtGui.QPixmap(r"C:\Users\intel\Desktop\Wally\Background.png")
background = background.scaled(1357, 728)
self.label.setPixmap(background)
self.label.show()

# red button
self.label1 = QLabel("", self)
red = QtGui.QPixmap(r"C:\Users\intel\Desktop\Wally\red.png")
red = red.scaled(11, 11)
self.label1.setPixmap(red)
self.label1.show()
self.label1.move(11,11)
self.label1.setCursor(QCursor(QtCore.Qt.PointingHa ndCursor))

# yellow button
self.label2 = QLabel("", self)
yellow = QtGui.QPixmap(r"C:\Users\intel\Desktop\Wally\yellow.png")
yellow = yellow.scaled(11, 11)
self.label2.setPixmap(yellow)
self.label2.show()
self.label2.move(30,11)
self.label2.setCursor(QCursor(QtCore.Qt.PointingHa ndCursor))

# green button
self.label3 = QLabel("", self)
green = QtGui.QPixmap(r"C:\Users\intel\Desktop\Wally\green.png")
green = green.scaled(19, 11)
self.label3.setPixmap(green)
self.label3.show()
self.label3.move(44,11)
self.label3.setCursor(QCursor(QtCore.Qt.PointingHa ndCursor))
self.label3.mousePressEvent = self.min

self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackgrou nd)
self.offset = None

def min(self):
self.setWindowState(QWindow.Minimized)

def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(1920, 1000)
#MainWindow.move(50,50)


if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = Ui_MainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())