PyQt5 – Set minimum window size | setMinimumWidth and setMinimumHeight method
Last Updated :
26 Mar, 2020
When we create a window, by default the window size is resizable although, we can use setMinimumSize() method to set the minimum size of the window. But what if we want to set minimum length only for width or height only, in order to do so we use setMinimumWidth()
method to set minimum width and setMinimumHeight()
method to set minimum height. When we use these method other length will be variable i.e there will be no minimum length to it, it can shrinks as much it can.
Syntax :
self.setMinimumWidth(width)
self.setMinimumHeight(height)
Argument : Both takes integer as argument.
Action performed.
setMinimumWidth()
sets the minimum width.
setMinimumHeight()
sets the minimum height.
Code for setting minimum 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
height = 200
self .setMinimumWidth(width)
self .label_1 = QLabel( "Minimum 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 setting minimum 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
height = 200
self .setMinimumHeight(height)
self .label_1 = QLabel( "Minimum 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 :
Share your thoughts in the comments
Please Login to comment...