Python PyQt5固定窗口大小

Python PyQt5固定窗口大小,python,pyqt,pyqt5,qt-designer,Python,Pyqt,Pyqt5,Qt Designer,我正在尝试将我的窗口/Q对话框设置为不可调整大小 我发现了下面的例子 self.setFixedSize(self.size()) 我不知道如何实现这一点。我可以将其放入由Qt Designer生成的.py文件中,或显式使用: QtWidgets.QDialog().setFixedSize(self.size()) 没有错误,但它不工作。谢谢。分别在加载UI(如果使用.UI文件)后或在窗口的init()中。应该是这样的: class MyDialog(QtWidgets.QDialog):

我正在尝试将我的窗口/Q对话框设置为不可调整大小

我发现了下面的例子
self.setFixedSize(self.size())

我不知道如何实现这一点。我可以将其放入由Qt Designer生成的.py文件中,或显式使用:

QtWidgets.QDialog().setFixedSize(self.size())
没有错误,但它不工作。谢谢。

分别在加载UI(如果使用.UI文件)后或在窗口的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_())
经验:

使用
MainWindow
对象设置固定窗口

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

这里的技巧是将固定大小属性指定给正确的对象。IIRC,当您使用pyuic5从设计器文件生成代码时,该类派生自对象,而不是您想要的窗口类型,因此导致错误行为。我明白了,谢谢。我仍然无法使用它,例如在默认的.ui到.py文件中<代码>从PyQt5导入QtCore、QtGui、QtWidgets类Ui_myfirstgui(对象):def setupUi(self,myfirstgui):myfirstgui.setObjectName(“myfirstgui”)myfirstgui.resize(411247)self.buttonBox=QtWidgets.QDialogButtonBox(myfirstgui)self.buttonBox.setGeometry(QtCore.QRect(20210381,32))self.buttonBox.setOrientation(QtCore.Qt.Horizontal)我知道原因了。resize()只为窗口设置一个特定的大小,它不会锁定用户以后调整它的大小。代码中没有可调用setupUi()的入口点。PUIC5生成了可怕的代码,包含了大量不必要的/违反直觉的东西。不要使用它而不进行修改,请参阅我的编辑以供参考。非常感谢。我也很感谢你的评论,肯定有助于为一个新的PyQt用户澄清一些事情。另外,我可以理解你在不必要的代码方面的意思。例如,您的最后一部分有pyuic生成的5对7
    self.height =300
    self.width =300
    self.top =50
    self.left =50