📜  PyQt5 - 为高度或宽度设置固定窗口大小

📅  最后修改于: 2022-05-13 01:55:08.971000             🧑  作者: Mango

PyQt5 - 为高度或宽度设置固定窗口大小

当我们创建一个窗口时,默认情况下窗口大小是可调整的,虽然我们可以使用setFixedSize()方法来设置窗口的固定大小,但是如果我们只想设置固定的高度或宽度的长度,我们不能使用这个方法。我们想将一个长度设置为固定,另一个是可变的,为此我们必须使用setFixedWidth()方法来设置宽度的固定长度和setFizedHeight()方法来设置高度的固定长度。

固定宽度代码 –

self.setFixedWidth(width)
self.setFixedHeight(height)

输出 :
pyqt-window-setFixedWidth固定高度代码 –

# 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
          
        # setting  the fixed width of window
        self.setFixedWidth(width)
  
  
        # creating a label widget
        self.label_1 = QLabel("Fixed 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())

输出 :
pyqt-window-setFixedHeight