PyQt5 – 设置窗口的最小尺寸 – setMinimumWidth和setMinimumHeight方法

PyQt5 – 设置窗口的最小尺寸 – setMinimumWidth和setMinimumHeight方法

当我们创建一个窗口时,虽然默认情况下窗口的大小是可以调整的,我们可以使用setMinimumSize()方法来设置窗口的最小尺寸。但是,如果我们只想设置宽度或高度的最小长度,为了做到这一点,我们使用setMinimumWidth()方法来设置最小宽度,setMinimumHeight()方法来设置最小高度。当我们使用这些方法时,其他长度将是可变的,即没有最小长度,它可以尽可能地缩小。

语法:

self.setMinimumWidth(width)
self.setMinimumHeight(height)

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

setMinimumWidth() 设置最小宽度。

setMinimumHeight() 设置最小高度

设置最小宽度的代码。

# 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
        height = 200
  
        # setting the minimum width
        self.setMinimumWidth(width)
  
        # creating a label widget
        self.label_1 = QLabel("Minimum 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 - 设置窗口的最小尺寸 - setMinimumWidth和setMinimumHeight方法

设置最小宽度的代码。

# 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
        height = 200
  
        # setting the minimum width
        self.setMinimumHeight(height)
  
        # creating a label widget
        self.label_1 = QLabel("Minimum 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 - 设置窗口的最小尺寸 - setMinimumWidth和setMinimumHeight方法

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程