PyQt5 固定 Window 大小

PyQt5 Fixed Window Size

我正在尝试将我的 window / QDialog 设置为不可调整大小。

我找到了下面的例子 self.setFixedSize(self.size())

我不确定如何实现它。我可以将它放在 Qt Designer 生成的 .py 文件中或明确使用:

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

没有错误,但它不工作。谢谢

分别加载 UI(如果使用 .ui 文件)或 window 的 init() 后。应该是这样的:

class MyDialog(QtWidgets.QDialog):

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

让我知道这是否适合你。

编辑:这是重新格式化所提供代码的方式。

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_())

表达式:

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

MainWindow对象设置固定的window

    self.height =300
    self.width =300
    self.top =50
    self.left =50