Results 1 to 3 of 3

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

  1. #1
    Join Date
    Jan 2020
    Location
    Jaipur, Rajasthan
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Question How to add a custom minimize button in PyQt5 as i am getting a TypeError?

    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:
    Qt Code:
    1. TypeError: min() takes 1 positional argument but 2 were given
    To copy to clipboard, switch view to plain text mode 
    Here is my code:

    Qt Code:
    1. from PyQt5 import QtWidgets, QtCore, QtGui
    2. from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QWidget
    3. from PyQt5.QtGui import QCursor, QWindow
    4.  
    5. class Ui_MainWindow(QWidget):
    6. def __init__(self):
    7. super().__init__()
    8. self.setWindowTitle("Wally")
    9. self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
    10.  
    11. # background
    12.  
    13. self.label = QLabel("", self)
    14. background = QtGui.QPixmap(r"C:\Users\intel\Desktop\Wally\Background.png")
    15. background = background.scaled(1357, 728)
    16. self.label.setPixmap(background)
    17. self.label.show()
    18.  
    19. # red button
    20.  
    21. self.label1 = QLabel("", self)
    22. red = QtGui.QPixmap(r"C:\Users\intel\Desktop\Wally\red.png")
    23. red = red.scaled(11, 11)
    24. self.label1.setPixmap(red)
    25. self.label1.show()
    26. self.label1.move(11,11)
    27. self.label1.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
    28.  
    29. # yellow button
    30.  
    31. self.label2 = QLabel("", self)
    32. yellow = QtGui.QPixmap(r"C:\Users\intel\Desktop\Wally\yellow.png")
    33. yellow = yellow.scaled(11, 11)
    34. self.label2.setPixmap(yellow)
    35. self.label2.show()
    36. self.label2.move(30,11)
    37. self.label2.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
    38.  
    39. # green button
    40.  
    41. self.label3 = QLabel("", self)
    42. green = QtGui.QPixmap(r"C:\Users\intel\Desktop\Wally\green.png")
    43. green = green.scaled(19, 11)
    44. self.label3.setPixmap(green)
    45. self.label3.show()
    46. self.label3.move(44,11)
    47. self.label3.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
    48. self.label3.mousePressEvent = self.min
    49.  
    50. self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
    51. self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
    52. self.offset = None
    53.  
    54. def min(self):
    55. self.setWindowState(QWindow.Minimized)
    56.  
    57. def setupUi(self, MainWindow):
    58. MainWindow.setObjectName("MainWindow")
    59. MainWindow.resize(1920, 1000)
    60. #MainWindow.move(50,50)
    61.  
    62.  
    63. if __name__ == "__main__":
    64. import sys
    65. app = QtWidgets.QApplication(sys.argv)
    66. MainWindow = Ui_MainWindow()
    67. ui = Ui_MainWindow()
    68. ui.setupUi(MainWindow)
    69. MainWindow.show()
    70. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,239
    Thanks
    303
    Thanked 865 Times in 852 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to add a custom minimize button in PyQt5 as i am getting a TypeError?

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to add a custom minimize button in PyQt5 as i am getting a TypeError?

    The min() function is, indeed, a standard Python function but it not the root cause of the message.

    The mousePressEvent() function has two arguments:
    • the object pointer
    • the QMouseEvent

    Your implementation only takes one argument.
    This works better:
    Qt Code:
    1. def min(self, event):
    2. self.setWindowState(self.windowState() | QWindow.Minimized)
    3. event.accept()
    To copy to clipboard, switch view to plain text mode 
    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
    Last edited by ChrisW67; 2nd January 2020 at 23:42.

  4. The following user says thank you to ChrisW67 for this useful post:

    d_stranz (3rd January 2020)

Similar Threads

  1. How to add a custom minimize button in PyQt5?
    By AbhaySalvi in forum Newbie
    Replies: 0
    Last Post: 2nd January 2020, 09:54
  2. Replies: 3
    Last Post: 8th September 2018, 09:15
  3. Minimize button problem
    By lin3398032 in forum Newbie
    Replies: 0
    Last Post: 16th March 2018, 18:22
  4. Reimplementation Minimize button
    By clousque in forum Newbie
    Replies: 5
    Last Post: 13th May 2014, 15:21
  5. Replies: 1
    Last Post: 5th September 2012, 07:16

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.