[PyQt] Multiple QMainWindow

Phil Thompson phil at riverbankcomputing.com
Tue Jul 7 11:12:15 BST 2009


On Tue, 7 Jul 2009 02:55:06 -0700 (PDT), gizmoog <oguillemot at polenn.com>
wrote:
> I'm new in python and pyQt.
> I try to launch a second MainWindow from a first MainWindow but it
doesn't
> work. The widget just pops and disappears immediatly.
> The following code works but that's not what I want...
> 
> /usr/bin/python
> #-*-coding: utf-8 -*-
> from PyQt4.QtGui import *
> from PyQt4.QtCore import *
> 
> import os,sys
> 
> #! /usr/bin/python
> #-*-coding: utf-8 -*-
> from PyQt4.QtGui import *
> 
> 
> class fenPrincipale(QMainWindow):
>     def __init__(self):
>         QMainWindow.__init__(self)
> 
> if __name__=="__main__":
>     a=QApplication(sys.argv)
>     f=fenPrincipale()
>     f.show()
>     f2=fenPrincipale()
>     f2.show()
>     
>     sys.exit(a.exec_())
> 
> and this one doesn't work:
> 
> 
> #! /usr/bin/python
> #-*-coding: utf-8 -*-
> from PyQt4.QtGui import *
> from PyQt4.QtCore import *
> 
> import os,sys
> 
> #! /usr/bin/python
> #-*-coding: utf-8 -*-
> from PyQt4.QtGui import *
> 
> 
> class fenPrincipale(QMainWindow):
>     def __init__(self):
>         QMainWindow.__init__(self)
>         
>         f2=fenSecondaire()
>         f2.show()
>         
> class fenSecondaire(QMainWindow):
>     def __init__(self):
>         QMainWindow.__init__(self)
> 
> if __name__=="__main__":
>     a=QApplication(sys.argv)
>     f=fenPrincipale()
>     f.show()
>     sys.exit(a.exec_())
> 
> 
> Could you help me ??

You aren't keeping a reference to f2 so it is being garbage collected as
soon as __init__() returns.

Phil


More information about the PyQt mailing list