When we create a window, by default the window size is resizable although, we can use setFixedSize() method to set the fixed size of the window but if we want to set only fix length of height or width only we cant use this method. We want to set one length fixed and other be variable in order to do so we have to use setFixedWidth() method to set fix length of width and setFizedHeight() method to set fix length of height.
Syntax :
self.setFixedWidth(width)
self.setFixedHeight(height)
Argument : Both take integer as argument. Action performed: setFixedWidth() sets the constant width. setFixedHeight() sets the constant height.
Code for fixed width –
Python3
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class Window(QMainWindow):
def __init__( self ):
super ().__init__()
self .setWindowTitle("Python")
width = 500
self .setFixedWidth(width)
self .label_1 = QLabel("Fixed width", self )
self .label_1.move( 0 , 0 )
self .label_1.setStyleSheet("border : 3px solid black;")
self .label_1.resize( 120 , 80 )
self .show()
App = QApplication(sys.argv)
window = Window()
sys.exit(App. exec ())
|
Output :
Code for fixed height –
Python3
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
class Window(QMainWindow):
def __init__( self ):
super ().__init__()
self .setWindowTitle("Python")
height = 400
self .setFixedHeight(height)
self .label_1 = QLabel("Fixed height", self )
self .label_1.move( 0 , 0 )
self .label_1.setStyleSheet("border : 3px solid black;")
self .label_1.resize( 120 , 80 )
self .show()
App = QApplication(sys.argv)
window = Window()
sys.exit(App. exec ())
|
Output : 