简体   繁体   中英

Minimize PyQt5 app with a button click

i found some material on how to minimize a pyqt widget to system tray, but i did not manage to find what i need.

I would like to minimize a widget on a button click, i do not want to overwrite default close button, when person clicks window border close, it will still close, but when it clicks my button, it will minimize to system tray.

Is this even possible, since all examples try to overwrite default event. This code i am attaching was created in qtDesigner and it holds just one button,sort of like playground, i hope someone could help, Cheers.

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

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

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QSystemTrayIcon
class Ui_Form(object):
    def setupUi(self, Form):


        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.btn_name = QtWidgets.QPushButton(Form)
        self.btn_name.setGeometry(QtCore.QRect(300, 30, 98, 33))
        self.btn_name.setObjectName("btn_name")
        self.btn_name.clicked.connect(self.minimize)
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.btn_name.setText(_translate("Form", "PushButton"))

    def minimize(self):
        print("Test")  




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

Qt Designer creates a class that is used to fill another widget, it is not a widget, so it is recommended to create a class that inherits from the appropriate widget and fills in using the Ui_Form class. For more information read the following:

Going to the point, you must use the showMinimized() method.

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

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

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(400, 300)
        self.btn_name = QtWidgets.QPushButton(Form)
        self.btn_name.setGeometry(QtCore.QRect(300, 30, 98, 33))
        self.btn_name.setObjectName("btn_name")        
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.btn_name.setText(_translate("Form", "PushButton"))

class Form(QtWidgets.QWidget, Ui_Form):
    def __init__(self, *args, **kwargs):
        QtWidgets.QWidget.__init__(self, *args, **kwargs)
        self.setupUi(self)
        self.btn_name.clicked.connect(self.minimize)

    def minimize(self):
        self.showMinimized()

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Form()
    w.show()
    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM