Search code examples
pythonpyqtpyqt5qt-designer

PyQt5 Fixed Window Size


I am trying to set my window / QDialog to not be resizable.

I found the following example self.setFixedSize(self.size())

I'm not sure how to implement this. I can put it in the .py file generated from Qt Designer or explicitly using:

QtWidgets.QDialog().setFixedSize(self.size())

without errors but it's not working. Thanks.


Solution

  • After loading your UI (if you use .ui file) or in init() of your window, respectively. It should be like this:

    class MyDialog(QtWidgets.QDialog):
    
        def __init__(self):
            super(MyDialog, self).__init__()
            self.setFixedSize(640, 480)
    

    Let me know if this works for you.

    Edit: this is how the provided code should be reformatted to work.

    from PyQt5 import QtWidgets 
    
    
    # It is considered a good tone to name classes in CamelCase.
    class MyFirstGUI(QtWidgets.QDialog): 
    
        def __init__(self):
            # Initializing QDialog and locking the size at a certain value
            super(MyFirstGUI, self).__init__()
            self.setFixedSize(411, 247)
    
            # Defining our widgets and main layout
            self.layout = QtWidgets.QVBoxLayout(self)
            self.label = QtWidgets.QLabel("Hello, world!", self)
            self.buttonBox = QtWidgets.QDialogButtonBox(self) 
            self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel | QtWidgets.QDialogButtonBox.Ok)
    
            # Appending our widgets to the layout
            self.layout.addWidget(self.label)
            self.layout.addWidget(self.buttonBox)
    
            # Connecting our 'OK' and 'Cancel' buttons to the corresponding return codes
            self.buttonBox.accepted.connect(self.accept)
            self.buttonBox.rejected.connect(self.reject)
    
    if __name__ == '__main__':
        import sys
        app = QtWidgets.QApplication(sys.argv)
        gui = MyFirstGUI()
        gui.show()
        sys.exit(app.exec_())