0

i want to display a message in pop-up window in python ...so i wrote this code...please check

import sys
from PyQt4.Qt import *

class MyPopup(QWidget):
    def __init__(self):
        print "6"
        QWidget.__init__(self)


class MainWindow(QMainWindow):
    def __init__(self, *args):
        print "4"
        QMainWindow.__init__(self, *args)
        self.cw = QWidget(self)
        self.setCentralWidget(self.cw)
        self.btn1 = QPushButton("Start Chat", self.cw)
        self.btn1.setGeometry(QRect(50, 30, 100, 30))
        self.connect(self.btn1, SIGNAL("clicked()"), self.doit)
        self.w = None

    def doit(self):
        print "5"
        print "Opening a new popup window..."
        self.w = MyPopup()
        self.w.setGeometry(QRect(0, 0, 400, 200))
        self.w.show()

class App(QApplication):
    def __init__(self, *args):
        print "3"
        QApplication.__init__(self, *args)
        self.main = MainWindow()
        #self.connect(self, SIGNAL("lastWindowClosed()"), self.byebye )
        self.main.show()

    #def byebye( self ):
        #self.exit(0)

for i in range(1, 5):
    if __name__ == "__main__":
        print "1"
        global app
        app = App(sys.argv)
        app.exec_()
    #main(sys.argv)
else:
    print "over"

Here first loop its working but from second loop i m getting segmentation fault...guys please help me..

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
user1372331
  • 37
  • 1
  • 5

1 Answers1

0

There is supposed to be just one QApplication object in an application. I guess your problem is that you attempt to create several in a loop.

If you want your user to close the main window four times before it actually closes, you can add an event handler:

class MainWindow(QMainWindow):
    def __init__(self, *args):
        ...
        self.counter = 1

    def closeEvent(self, event):
        print "closeEvent", self.counter
        self.counter += 1
        if self.counter < 5:
            event.ignore()
        else:
            event.accept()
Janne Karila
  • 24,266
  • 6
  • 53
  • 94