0

我想在 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"

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

4

1 回答 1

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()
于 2012-05-29T10:28:57.223 回答