Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 460
Alex Avatar asked Jan 02 '23 19:01

Alex


2 Answers

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_())
like image 82
Дмитрий Клименко Avatar answered Jan 24 '23 11:01

Дмитрий Клименко


exp:

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(700, 494)
        MainWindow.setFixedSize(300,300) :

Set the fixed window with MainWindow object

    self.height =300
    self.width =300
    self.top =50
    self.left =50
like image 25
TAKKA ALBERT Avatar answered Jan 24 '23 11:01

TAKKA ALBERT