PyQT5之視窗最大化最小化

星空28發表於2024-06-11

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class WindowMaxMin(QWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.resize(500, 300)
        self.setWindowTitle("視窗最大化最小化")
        # self.setWindowFlags(Qt.WindowMaximizeButtonHint | Qt.WindowStaysOnTopHint)
        # self.setWindowFlags(Qt.WindowMaximizeButtonHint)
        # 最大化、最小化、關閉
        # self.setWindowFlags(Qt.WindowMaximizeButtonHint | Qt.WindowMinimizeButtonHint | Qt.WindowCloseButtonHint)
        # self.setObjectName("MainWindow")
        # self.setStyleSheet("#MainWindow{border-image:url(image/img1.png);}")

        maxButton1 = QPushButton("視窗最大化1")
        maxButton2 = QPushButton("視窗最大化2")
        maxButton3 = QPushButton("視窗最小化")

        layout = QVBoxLayout()
        layout.addWidget(maxButton1)
        layout.addWidget(maxButton2)
        layout.addWidget(maxButton3)
        self.setLayout(layout)

        maxButton1.clicked.connect(self.maximized1)
        maxButton2.clicked.connect(self.showMaximized)
        maxButton3.clicked.connect(self.showMinimized)

    def maximized1(self):
        desktop = QApplication.desktop()
        # 獲取桌面可用尺寸
        rect = desktop.availableGeometry()
        # print(rect)
        self.setGeometry(rect)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = WindowMaxMin()
    main.show()
    sys.exit(app.exec_())

相關文章