PyQt5 – 设置窗口宽度或高度的最大尺寸

PyQt5 – 设置窗口宽度或高度的最大尺寸

当我们创建一个窗口时,默认情况下窗口的大小是可以调整的,尽管我们可以使用setMaximumSize()方法来设置窗口的最大尺寸。但是如果我们想只为宽度或高度设置最大长度呢。为了做到这一点,我们使用setMaximumWidth()和setMaximumHeight()方法来设置最大宽度/高度。当我们使用这些方法时,其他长度将是可变的,即没有最大长度,它可以被拉伸到屏幕的大小。

语法:

self.setMaximumWidth(width)
self.setMaximumHeight(height)

参数: 两者都以整数作为参数。

执行的操作:

setMaximumWidth() 设置最大宽度。 ‘

setMaximumHeight() 设置最大高度。

最大宽度的代码

# 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 = 200
  
        # setting the maximum width
        self.setMaximumWidth(width)
  
        # creating a label widget
        self.label_1 = QLabel("Maximum width", 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 - 设置窗口宽度或高度的最大尺寸

最大高度的代码 –

# 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")
  
        height = 200
  
        # setting the maximum height
        self.setMaximumHeight(height)
  
  
        # creating a label widget
        self.label_1 = QLabel("Maximum height", 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教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程