python分段错误

时间:2012-05-29 10:07:54

标签: python pyqt pyqt4

我想在python的弹出窗口中显示一条消息...所以我写了这段代码......请检查

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"

这里首先循环它的工作但是从第二个循环我得到分段错误...伙计们请帮帮我..

1 个答案:

答案 0 :(得分:0)

应用程序中应该只有一个QApplication对象。我猜你的问题是你试图在循环中创建几个。

如果您希望用户在实际关闭之前关闭主窗口四次,可以添加一个事件处理程序:

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()