PyQt5固定窗口大小

时间:2018-09-26 12:13:00

标签: python pyqt pyqt5 qt-designer

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

我找到了以下示例     self.setFixedSize(self.size())

我不确定该如何实现。我可以将其放在Qt Designer生成的.py文件中,也可以使用以下命令显式放置:

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

没有错误,但是没有用。谢谢。

2 个答案:

答案 0 :(得分:2)

分别在加载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_())

答案 1 :(得分:1)

exp:

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

使用MainWindow对象设置固定窗口

    self.height =300
    self.width =300
    self.top =50
    self.left =50
相关问题