在Python中创建新实例之前,QApplication单例错误

时间:2019-07-08 08:55:11

标签: python python-3.x pyside2

我正在尝试使用适用于Python和PySide2的QT构建简单的弹出式GUI。

我创建了一个名为“ Ventana”的类,该类在其 init 函数中设置了一个新的QApplication。


class Ventana():

  def __init__(self, title, text):
    self.app = QApplication([])
    self.win = QWidget()
    self.grid = QGridLayout()
    #WINDOW SETUP...
    ...

  def message(self, icon):
    self.icon.setPixmap(QPixmap(f"icons/{icon}.png"))
    buttonOK = QPushButton("Ok", self.win)
    buttonOK.clicked.connect(self.win.close)
    self.grid.addWidget(buttonOK, 3, 3)
    self.win.setLayout(self.grid)
    self.win.show()
    self.app.exec_()

  def tabla(self, icono, lista):
    text = '<table style="width:100%"><tr>'
    table = QLabel(self.win)
    column = 0
    for element in list:
      if column >= 3:
        text += '</tr><tr>'
      else:
        text += f'<th>{element}</th>'
    text += '</tr></table>'
    table.setText(text)
    self.grid.addWidget(table, 2, 2)
    self.win.show()
    self.app.exec_()

if __name__ == "__main__":
  ret1 = Ventana("Lorem Ipsum",
                 "Lorem ipsum dolor sit amet, consectetur adipiscing elit." +
                 " Aliquam nec sapien vitae metus maximus tincidunt. " +
                 "Quisque egestas imperdiet massa, condimentum dictum elit" +
                 " dictum vel.").message("like")
  print (ret1)

  # ERROR HAPPENS IN __init__ OF THIS INSTANCE
  ret2 = Ventana("Lorem Ipsum",
                 "Lorem ipsum dolor sit amet, consectetur adipiscing elit." +
                 " Aliquam nec sapien vitae metus maximus tincidunt. " +
                 "Quisque egestas imperdiet massa, condimentum dictum elit" +
                 " dictum vel.").tabla("upload", sampleList)
  print (ret2)

当我尝试ro实例化第二个“ Ventana”对象时,出现如下错误消息:

  

RuntimeError:请在销毁QApplication单例之前   创建一个新的QApplication实例。

0 个答案:

没有答案