When we create a window, by default the window size is resizable, although we can use setMaximumSize()
method to set the maximum size of the window. But what if we want to set maximum length only for width or height only. In order to do so we use setMaximumWidth()
and setMaximumHeight()
method to set maximum width / height. When we use these method other length will be variable i.e there will be no maximum length to it, it can be stretched to the size of screen.
Syntax :
self.setMaximumWidth(width)
self.setMaximumHeight(height)
Argument : Both takes integer as argument.
Action performed :
setMaximumWidth()
sets the maximum width.
setMaximumHeight()
sets the maximum height.
Code for maximum width –
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 = 200
self .setMaximumWidth(width)
self .label_1 = QLabel( "Maximum 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 maximum height –
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 = 200
self .setMaximumHeight(height)
self .label_1 = QLabel( "Maximum 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 :
