可以通過自定義函數實現窗口的最大化,即self.setGeometry(rect)設置窗口為桌面的尺寸
也可以直接調用系統的程序進行實現
WindowMaxMin.py
""" 用代碼控制窗口的最大值和最小值 """ from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * import sys class WindowMaxMin(QWidget): ### 構造函數 def __init__(self, parent=None): """構造函數""" #調用父類構造函數 super(WindowMaxMin, self).__init__() self.resize(300, 400) self.setWindowTitle("用代碼控制窗口的最大化和最小化") self.setWindowFlags(Qt.WindowMaximizeButtonHint | Qt.WindowMinimizeButtonHint | Qt.WindowCloseButtonHint) layout = QVBoxLayout() maxButton1 = QPushButton() maxButton1.setText("窗口最大化1") maxButton1.clicked.connect(self.maximized1) maxButton2 = QPushButton() maxButton2.setText("窗口最大化2") #self.showMaximized(最大化操作) maxButton2.clicked.connect(self.showMaximized) maxButton3 = QPushButton() maxButton3.setText("窗口最小化") #self.showMaximized(最大化操作) maxButton3.clicked.connect(self.showMinimized) layout.addWidget(maxButton1) layout.addWidget(maxButton2) layout.addWidget(maxButton3) self.setLayout(layout) def maximized1(self): desktop = QApplication.desktop() #獲取桌面可用尺寸 rect = desktop.availableGeometry() self.setGeometry(rect) if __name__ == "__main__": app = QApplication(sys.argv) main = WindowMaxMin() main.show() sys.exit(app.exec_())