Loading a pyqt application causes a segmentation error several times

I have a file Foo.pythat contains the code below. When I run the file from the command line using python Foo.py, everything works. However, if I use python CLI

python 
import Foo
Foo.main()
Foo.main()
Foo.main()

The first call works fine, the second calls all the hell warnings, the first of which

(python:5389): Gtk-CRITICAL **: IA__gtk_container_add: assertion 'GTK_IS_CONTAINER (container)' failed

And the last reason for segmentation error. What is the problem with my code?

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import os
from PyQt4 import Qt
from PyQt4 import QtGui,QtCore

class Foo (QtGui.QWidget):

    def __init__(self,parent=None):
        super(Foo,self).__init__()
        self.setUI()
        self.showMaximized()


    def setUI(self):
        self.setGeometry(100,100,1150,650)
        self.grid = QtGui.QGridLayout()
        self.setLayout(self.grid)

        #For convininece, I set different ui "concepts" in their own function
        self.setInterfaceLine()
        self.setMainText()



    def setMainText(self):
        #the main box, where information is displayed
        self.main_label = QtGui.QLabel('Details')
        self.main_text = QtGui.QLabel()
        self.main_text.setAlignment(QtCore.Qt.AlignTop)
        #Reading the welcome message from file

        self.main_text.setText('')


        self.main_text.setWordWrap(True) #To handle long sentenses
        self.grid.addWidget(self.main_text,1,1,25,8)


    def setInterfaceLine(self):

        #Create the interface section
        self.msg_label = QtGui.QLabel('Now Reading:',self)
        self.msg_line = QtGui.QLabel('commands',self) #the user message label
        self.input_line = QtGui.QLineEdit('',self) #The command line
        self.input_line.returnPressed.connect(self.executeCommand)
        self.grid.addWidget(self.input_line,26,1,1,10)
        self.grid.addWidget(self.msg_label,25,1,1,1)
        self.grid.addWidget(self.msg_line,25,2,1,7)


    def executeCommand(self):
        fullcommand = self.input_line.text() #Get the command
        args = fullcommand.split(' ')
        if fullcommand =='exit':
            self.exit()





    def exit(self):
        #Exit the program, for now, no confirmation
        QtGui.QApplication.quit()



def main():
    app = QtGui.QApplication(sys.argv)
    foo = Foo(sys.argv)
    app.exit(app.exec_())

if __name__ in ['__main__']:
    main()
0
source share
3 answers

I can play in Python 3, but not Python 2.

- QApplications. Qt , QApplications, , , , - . main() QApplication , -, , , main().

QApplication . , QApplication, , , QApplication.instance(), , .

, main() :

def main():
    global app
    app = QtGui.QApplication.instance()
    if app is None:
        app = QtGui.QApplication(sys.argv)
    foo = Foo(sys.argv)
    app.exit(app.exec_())

, , , QApplication . , , , . , , , Python 2. global app , , .

, QApplication... , , , , , . PyQt, , , .

+3

QApplication. GUI .

+1

, , , , .

The problem is probably related to some garbage collection problems when the function returns main(the deletion order may be unpredictable).

Try adding del fooafter the event loop completes.

+1
source

Source: https://habr.com/ru/post/1615755/


All Articles