除了垂直布局,我想清除框架中的所有内容。我将在以后添加更多内容,将其清空后添加到“垂直”布局中。
我试图清除“垂直”布局,但出现错误:QVBoxLayout'对象没有属性“ clear
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import QPushButton, QLabel, QFrame, QVBoxLayout, QWidget, QGridLayout
class MyWindow(QtWidgets.QMainWindow, QPushButton):
def __init__(self):
super(MyWindow, self).__init__()
centralWidget = QWidget()
self.setCentralWidget(centralWidget)
self.Frame = QFrame(self)
self.Frame2 = QFrame(self)
self.button = QPushButton("Button 1",self)
self.button2 = QPushButton("Clear",self)
self.button2.clicked.connect(self.Clear)
self.label = QLabel("Label 1",self)
self.Glayout = QGridLayout(centralWidget)
self.Glayout.addWidget(self.Frame)
self.Glayout.addWidget(self.Frame2)
self.VL = QVBoxLayout(self.Frame)
self.VL2 = QVBoxLayout(self.Frame2)
self.VL.addWidget(self.button)
self.VL.addWidget(self.label)
self.VL2.addWidget(self.button2)
def Clear(self):
self.VL2.clear()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())