PyQt5 – 如何设置窗口的最小尺寸 | setMinimumSize 方法
我们知道我们可以调整 PyQt5 应用程序的窗口大小,但是在缩小窗口大小的同时,我们可以设置最小大小,使其不能再缩小。在本文中,我们将看到如何做到这一点。
为此,我们将使用setMinimumSize()
方法。
Syntax : self.setMinimumSize(width, height)
Argument : It takes two integer as argument.
Action performed : It sets the minimum size of window.
代码 :
# 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 size
self.setMinimumSize(width, height)
# creating a label widget
self.label_1 = QLabel("Minimum 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())
输出 :