PyQt5 QGridLayout在删除小部件时崩溃

时间:2020-05-29 20:26:34

标签: python pyqt pyqt5

我有一个小的应用程序,该应用程序从服务器获取文件名并将其显示给用户。但是,我遇到了一个使我的项目完全崩溃的问题。这是代码,它在newData函数中的某处失败,我尝试了多种清除布局的方法,但没有一种起作用。

from PyQt5 import QtCore
from PyQt5.uic import loadUi
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QPushButton, QMainWindow, QGridLayout, QLabel, QVBoxLayout


class MainMenu(QMainWindow):
    """The main menu class"""

    def __init__(self):
        QMainWindow.__init__(self)
        self.gridLayout = QGridLayout(self)
        self.ui = loadUi("UI/MainMenuUI.ui", self)
        self.NameLabel.setText("NAME : NAME")
        self.setAutoFillBackground(True)
        self.RefreshButton.clicked.connect(self.RefreshFiles)

    def RefreshFiles(self):
        self.newData("FILE|FILE|fIlE|")

    def newData(self, data):
        print("New data called")
        if data == '':
            if self.gridLayout.count() > 1:
                w = self.gridLayout.itemAt(0).widget()
                self.gridLayout.removeWidget(w)
                w.hide()
        else:
            print("else")
            if self.gridLayout.count() > 1:
                w = self.gridLayout.itemAt(0)
                self.gridLayout.removeWidget(w)
            self.DisplayFiles(data)

    def DisplayFiles(self, data):
        print("Displaying files")
        print(data)
        data = data.split("|")[:len(data.split("|")) - 1]
        buttons = {}
        j, index, prev = 0, 0, 0
        for i in range(0, len(data)):
            if i % 3 == 0:
                j += 1
                index = 0
            index += 1
            img = "unknown.png"
            filename = str(data[prev])
            actions = {"edit": partial(print, filename),
                       "delete": partial(print, filename),
                       "download": partial(print, filename)
                       }

            def InitiateFileInterationButtons(name, actions):
                """Initiation of file interaction menu (actions is an dictionary)"""
                layout = QVBoxLayout()
                pxmp = QPixmap("unknown.png")
                lbl = QLabel()
                textlbl = QLabel(name)
                lbl.setPixmap(pxmp)
                lbl.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
                layout.addWidget(textlbl)
                layout.addWidget(lbl)
                sublayout = QHBoxLayout()
                deletebutton = QPushButton("delete")
                deletebutton.clicked.connect(actions["delete"])
                editbutton = QPushButton("edit")
                editbutton.clicked.connect(actions["edit"])
                downloadbutton = QPushButton("download")
                downloadbutton.clicked.connect(actions["download"])
                sublayout.addWidget(deletebutton)
                sublayout.addWidget(editbutton)
                sublayout.addWidget(downloadbutton)
                layout.addLayout(sublayout)
                return layout

            buttons[(index, j)] = InitiateFileInterationButtons(
                filename, actions)
            prev += 1
            self.gridLayout.addLayout(buttons[(index, j)], index, j)
            print("initiated {}".format(filename))

    def Show(self):
        self.ui.NameLabel.setText("NAME : NAME")
        self.RefreshFiles()
        self.ui.show()

app = QApplication([])
mainmenu = MainMenu()
mainmenu.Show()
app.exec_()

这是mainmenui.ui文件

<ui version="4.0">
 <class>UploadWindow</class>
 <widget class="QMainWindow" name="UploadWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1304</width>
    <height>684</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QLabel" name="NameLabel">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>161</width>
      <height>61</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>Bungee Inline</family>
      <pointsize>9</pointsize>
     </font>
    </property>
    <property name="text">
     <string>NAME:SAMPLETEXT</string>
    </property>
    <property name="textFormat">
     <enum>Qt::AutoText</enum>
    </property>
   </widget>
   <widget class="QPushButton" name="RefreshButton">
    <property name="geometry">
     <rect>
      <x>280</x>
      <y>0</y>
      <width>141</width>
      <height>71</height>
     </rect>
    </property>
    <property name="font">
     <font>
      <family>Bungee Inline</family>
     </font>
    </property>
    <property name="text">
     <string>Refresh fie list</string>
    </property>
   </widget>
   <widget class="QWidget" name="gridLayoutWidget">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>70</y>
      <width>1301</width>
      <height>551</height>
     </rect>
    </property>
    <layout class="QGridLayout" name="gridLayout"/>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>1304</width>
     <height>26</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

0 个答案:

没有答案