PyQt5 – 设置最大窗口尺寸

PyQt5 – 设置最大窗口尺寸

当我们创建一个窗口时,默认情况下,它是可以调整大小的,但在setFixedSize()方法的帮助下,我们可以固定窗口的大小。但是,如果我们想在一定程度上调整窗口的大小,为了做到这一点,我们必须设置窗口的最大尺寸。我们将使用setMaximumSize()方法。

语法: self.setMaximumSize(width, height)

参数: 它需要两个参数,都是整数,即宽度和高度。

执行的操作: 它设置窗口的最大尺寸。

代码。

# importing the required libraries
  
from PyQt5.QtCore import * 
from PyQt5.QtGui import * 
from PyQt5.QtWidgets import * 
import sys
  
  
class Window(QMainWindow):
    def __init__(self):
        super().__init__()
  
  
        # set the title
        self.setWindowTitle("Python")
  
        width = 500
        height = 400
        # setting the maximum size
        self.setMaximumSize(width, height)
  
  
        # creating a label widget
        self.label_1 = QLabel("Maximum size", self)
  
        # moving position
        self.label_1.move(0, 0)
  
        # setting up the border
        self.label_1.setStyleSheet("border :3px solid black;")
  
        # resizing label
        self.label_1.resize(120, 80)
  
        # show all the widgets
        self.show()
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
# start the app
sys.exit(App.exec())

输出 :

PyQt5 - 设置最大窗口尺寸

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程