簡體   English   中英

PyQt5 固定窗口大小

[英]PyQt5 Fixed Window Size

我正在嘗試將我的窗口 / QDialog 設置為不可調整大小。

我發現以下示例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_())

經驗:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM