在PyQt中删除小部件和布局

阿卡什DG

每当我单击“大小”按钮时,它只会清除“文本”字段。

我想清除所有布局以及小部件!

我是python编程的新手。你能帮我解决问题吗?

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

def __init__(self):
    super(Example, self).__init__()

    self.homeScreenBox = QtGui.QGridLayout()
    self.editTextScreenBox = QtGui.QVBoxLayout()


    vbox = QtGui.QGridLayout()
    vbox.addLayout(self.homeScreenBox,0,0)
    vbox.addLayout(self.editTextScreenBox,0,0)

    self.setLayout(vbox)

    self.homeScreen()
    self.showFullScreen()
    self.show()

def editTextScreen(self):

    self.mybox = QtGui.QHBoxLayout()
    self.mybox2 = QtGui.QHBoxLayout()
    self.editTextScreenBox.addLayout(self.mybox)
    self.editTextScreenBox.addLayout(self.mybox2)

    self.btnbtn = QtGui.QPushButton("LOL")
    self.mybox.addWidget(self.btnbtn)
    self.lolmlol = QtGui.QPushButton("lomlol")
    self.mybox2.addWidget(self.lolmlol)
    self.uloo = QtGui.QPushButton("looloolool")
    self.mybox.addWidget(self.uloo)




def keyPressEvent(self, event):
    if event.key() == QtCore.Qt.Key_Escape:
        self.close()

def homeScreen(self):
    self.home_box_up = QtGui.QVBoxLayout()
    self.home_box_down = QtGui.QVBoxLayout()

    self.homeScreenBox.addLayout(self.home_box_up,0,0)
    self.homeScreenBox.addLayout(self.home_box_down,1,0)

    self.h1 = QtGui.QHBoxLayout()
    self.h2 = QtGui.QHBoxLayout()
    self.h3 = QtGui.QHBoxLayout()

    self.home_box_down.addLayout(self.h1)
    self.home_box_down.addLayout(self.h2)
    self.home_box_down.addLayout(self.h3)

    self.txt_home = QtGui.QLabel("TEXT")
    self.home_box_up.addWidget(self.txt_home)

    self.btnSizeH = QtGui.QPushButton("Size")
    self.btnEditTextH = QtGui.QPushButton("Edit Text")
    self.btnDateH = QtGui.QPushButton("Set Date")
    self.btn4 = QtGui.QPushButton("Button 4")
    self.btn5 = QtGui.QPushButton("Button 5")
    self.btn6 = QtGui.QPushButton("Button 6")
    self.btn7 = QtGui.QPushButton("Button 7")
    self.btn8 = QtGui.QPushButton("Button 8")
    self.btn9 = QtGui.QPushButton("Button 9")
    self.h1.addWidget(self.btnSizeH)
    self.h1.addWidget(self.btnEditTextH)
    self.h1.addWidget(self.btnDateH)
    self.h2.addWidget(self.btn4)
    self.h2.addWidget(self.btn5)
    self.h2.addWidget(self.btn6)
    self.h2.addWidget(self.btn7)
    self.h3.addWidget(self.btn8)
    self.h3.addWidget(self.btn9)

    self.txt_home.setStyleSheet("font-size:24px;")        

    self.setStyleSheet("QPushButton{font-size:24px;padding:20px;background-color:transparent;\
                            border:2px solid #5585ff;border-radius:35%;box-shadow: 10px 10px;\
                            margin:10px}"
                       "QPushButton:pressed { background-color:grey }")

    self.connect(self.btnSizeH, QtCore.SIGNAL("clicked()"), self.home_edit)

def home_edit(self):
    self.remove_homeScreen(self.homeScreenBox)
    #self.editTextScreen()


def remove_homeScreen(self, layout):
    #for cnt in reversed(range(self.homeScreenBox.count())):
    #    widget = self.homeScreenBox.takeAt(cnt).layout()


    #    if widget is not None:
    #        widget.deleteLater()

    for i in reversed(range(layout.count())):
        item = layout.itemAt(i)

    if isinstance(item, QtGui.QWidgetItem):
        print "widget" + str(item)
        item.widget().close()
        # or
        # item.widget().setParent(None)
    elif isinstance(item, QtGui.QSpacerItem):
        print "spacer " + str(item)
        # no need to do extra stuff
    else:
        print "layout " + str(item)
        self.remove_homeScreen(item.layout())

    # remove the item from layout
    layout.removeItem(item)   




def main():    
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

如果有人有其他解决方案,请为多个向导布局应用程序建议代码。

阿卡什DG

我刚刚找到问题的答案!

只需替换为循环,

for i in reversed(range(layout.count())):
    item = layout.itemAt(i)

    if isinstance(item, QtGui.QWidgetItem):
        print "widget" + str(item)
        item.widget().close()

    elif isinstance(item, QtGui.QSpacerItem):
        print "spacer " + str(item)
    else:
        print "layout " + str(item)
        self.remove_homeScreen(item.layout())

    # remove the item from layout
    layout.removeItem(item)

这只是一些“空格”的错误!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章