python - 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。我想以最佳锻炼方式重构课程。错误在哪里?
解决方案
您应该在小部件中设置布局而不是将其设置为 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_())
推荐阅读
- entity-framework - 如何防止实体框架在数据库优先方法中将空字符串转换为 null
- python - 在 Flask 上注销时更改用户的布尔值
- c# - 从脚本返回值
- javascript - AWS Lambda /tmp 文件夹不在执行之间共享
- html - HTML表格不能改变文本的位置
- python - 如何将字典列表放入 Dataframe 的列中?
- reactjs - 限制儿童类型
- java - Java mail API on send mail where mail body应该有测试报告的摘要
- python-3.x - Python 和 Kivy:如何从内存中更新图像?
- elasticsearch - 如何在elasticsearch中获取文档的开始日期和结束日期字段之间的每一天的存储桶