PDA

View Full Version : How to add a custom minimize button in PyQt5 as i am getting a TypeError?



AbhaySalvi
2nd January 2020, 09:58
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_())

d_stranz
2nd January 2020, 16:34
Isn't "min()" a reserved name in Python? To return the minimum of two numerical values? Change the name of your min() slot to something else.

ChrisW67
2nd January 2020, 23:36
The min() function is, indeed, a standard Python function but it not the root cause of the message.

The mousePressEvent() (https://doc.qt.io/qt-5/qlabel.html#mousePressEvent) function has two arguments: the object pointer
the QMouseEvent
Your implementation only takes one argument.
This works better:

def min(self, event):
self.setWindowState(self.windowState() | QWindow.Minimized)
event.accept()
I am not entirely sure it a clean approach though.
I would also consider:
making the three buttons actual QPushButtons and doing this plumbing with signal/slot connections rather than hijacking the event handler
Using layouts to place widgets within the parent widget