首页 > 解决方案 > Python使用QGridLayout不添加小部件

问题描述

我是 Qt5 的新手,我有一个简单的 QGridLayout 布局掩码。我想创建一个窗口,其中小部件调整大小并调整窗口大小

这是代码

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QFileDialog ,QVBoxLayout,QGroupBox,QGridLayout

class MainWindow(QtWidgets.QMainWindow, QtWidgets.QFileDialog, QtWidgets.QLineEdit):
    def __init__(self):
        super().__init__()

        self.title = "Calcolo Hash"
        self.top = 100
        self.left = 100
        self.width = 800
        self.height = 330

        self.InitWindow()

    def InitWindow(self):
        self.setWindowIcon(QtGui.QIcon("icona_aprie.png"))
        self.setWindowTitle(self.title)
        self.setGeometry(self.top, self.left, self.width, self.height)

        self.creamaschera()

        self.show()

    def creamaschera(self):
        print ("creazione maschera")
        layout = QtWidgets.QGridLayout()
        self.txtcartella = QtWidgets.QLineEdit()
        self.lblprova = QtWidgets.QLabel("Please enter new name:")
        # self.txtcartella.setGeometry(QtCore.QRect(10, 10, 301, 20))
        # self.txtcartella.setObjectName("txtcartella")
        layout.addWidget(self.lblprova,0,0)
        layout.addWidget(self.txtcartella,0,1)

        self.setLayout(layout)
        # self.horizontalGroupBox.setLayout(layout)

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    #w.show()
    sys.exit(app.exec_())

但是当我运行时,面具是空的。我用 Qt5 设计器制作基础并将其转换为 python。我想以最佳锻炼方式重构课程。错误在哪里?

标签: pythonpyqt5qt-designer

解决方案


您应该在小部件中设置布局而不是将其设置为 MainWindow,因为您使用的是 MainWindow 类本身,并且在访问 MainWindow 类的方法和属性时,您可以更具体

import sys
from PyQt5 import QtCore, QtGui
from PyQt5.QtWidgets import (QLineEdit, QMainWindow, QWidget,
                             QGridLayout, QLabel, QApplication)


class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.title = "Calcolo Hash"
        self.top = 100
        self.left = 100
        self.width = 800
        self.height = 330
        self.InitWindow()
    def InitWindow(self):
        self.setWindowIcon(QtGui.QIcon("icona_aprie.png"))
        self.setWindowTitle(self.title)
        self.setGeometry(self.top, self.left, self.width, self.height)

        self.creamaschera()

    def creamaschera(self):
        print("creazione maschera")
        Layout = QGridLayout()
        self.txtcartella = QLineEdit()
        self.lblprova = QLabel("Enter Your Name")
        self.lblprova.setGeometry(QtCore.QRect(15, 15, 301, 20))
        self.txtcartella.setObjectName("txtcartella")
        Layout.addWidget(self.lblprova, 0, 0)
        Layout.addWidget(self.txtcartella, 0, 1)
        # Widget to setLayout in it since you are using MainWindow as an Class
        widget = QWidget()
        widget.setLayout(Layout)
        # SetCentralWidget without this widget won't be placed
        self.setCentralWidget(widget)
        # self.horizontalGroupBox.setLayout(layout)


if __name__ == "__main__":
    app = QApplication(sys.argv)
    MainWindow = MainWindow()
    MainWindow.show()
    sys.exit(app.exec_())

推荐阅读