7

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Alex
  • 81
  • 1
  • 1
  • 3

2 Answers2

13

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_())
  • The trick here is to assign fixed size property to the right object. IIRC, when you use pyuic5 to generate code from Designer file, the class derives from object, not your desired window type, hence leading to incorrect behaviour. – Дмитрий Клименко Sep 26 '18 at 12:32
  • I see, thanks. I'm still having trouble using it e.g. in the default .ui to .py file. `from PyQt5 import QtCore, QtGui, QtWidgets class Ui_myfirstgui(object): def setupUi(self, myfirstgui): myfirstgui.setObjectName("myfirstgui") myfirstgui.resize(411, 247) self.buttonBox = QtWidgets.QDialogButtonBox(myfirstgui) self.buttonBox.setGeometry(QtCore.QRect(20, 210, 381, 32)) self.buttonBox.setOrientation(QtCore.Qt.Horizontal) ` – Alex Sep 27 '18 at 03:43
  • I can see why. resize() only sets a certain size for the window, it doesn't lock user from resizing it later. There's no entrypoint in your code to call setupUi(). And pyuic5 generates horrible code, with lots of unnecessary/counterintuitive stuff. Refrain from using it without reworking and see my edit for reference. – Дмитрий Клименко Sep 27 '18 at 06:34
  • Thank you very much. I also appreciate the comments, definitely helped clear a few things up for a new PyQt user. – Alex Sep 28 '18 at 11:45
  • Also, I can see what you mean in terms of unnecessary code. E.g. the last section yours has 5 vs 7 that pyuic generates. – Alex Sep 28 '18 at 11:54
  • You're very welcome! If my question was useful to you, it would be nice of you to upvote it. – Дмитрий Клименко Sep 28 '18 at 11:55
  • Oh no, this one I added myself. This is an entrypoint for our application; first, we check if this module is launched as main, instead of imported somewhere, and then we initialize our QApplication, creating all necessary bindings, and defining then calling our UI. You can contact me directly if this was confusing, or I can make another answer with more details. – Дмитрий Клименко Sep 28 '18 at 11:58
  • Oh I see. A separate answer explaining that would be much appreciated. – Alex Sep 29 '18 at 07:59
  • How do you make it a certain percentage of the window height and width? – West Aug 17 '22 at 12:05
3

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
jlewkovich
  • 2,725
  • 2
  • 35
  • 49