Really strange behavior

Discussion in 'Python' started by IloChab, Nov 4, 2006.

  1. IloChab

    IloChab Guest

    Sorry I wasn't able to be more specific on my topic but I really do not
    know how to classify my problem, I mean that I can't understand if it's
    a python
    or a twisted
    or a Qt4
    problem
    I'm trying to run a simple application with Twisted and Qt4.
    To do this I downloaded this:
    http://twistedmatrix.com/trac/attachment/ticket/1770/qt4reactor.py
    Now,
    if I run this:
    # >>>>>>>>>
    import qt4reactor
    import sys
    from PyQt4 import QtGui
    from winIum import Window

    def main():
    app = QtGui.QApplication(sys.argv)
    qt4reactor.install(app)
    MainWindow = QtGui.QMainWindow()
    win = Window(MainWindow)
    MainWindow.show()
    from twisted.internet import reactor
    reactor.run()
    # <<<<<<<<
    my window shows and run correctly.

    If I run this:
    # >>>>>>>>>>>>>
    import qt4reactor
    import sys
    from PyQt4 import QtGui
    from winIum import Window

    def creApp():
    app = QtGui.QApplication(sys.argv)
    qt4reactor.install(app)
    retrun app
    def creWin():
    MainWindow = QtGui.QMainWindow()
    win = Window(MainWindow)
    MainWindow.show()
    def main():
    app = creApp()
    creWin()
    from twisted.internet import reactor
    reactor.run()
    # <<<<<<<<<<<<<
    my window doesn't show and the script doesn't stop but remains trapped in
    some gui loop.

    What's the problem I can't see??

    Thank you in advance for any help.
    Licia
     
    IloChab, Nov 4, 2006
    #1
    1. Advertising

  2. IloChab

    ArdPy Guest

    IloChab wrote:
    > Sorry I wasn't able to be more specific on my topic but I really do not
    > know how to classify my problem, I mean that I can't understand if it's
    > a python
    > or a twisted
    > or a Qt4
    > problem
    > I'm trying to run a simple application with Twisted and Qt4.
    > To do this I downloaded this:
    > http://twistedmatrix.com/trac/attachment/ticket/1770/qt4reactor.py
    > Now,
    > if I run this:
    > # >>>>>>>>>
    > import qt4reactor
    > import sys
    > from PyQt4 import QtGui
    > from winIum import Window
    >
    > def main():
    > app = QtGui.QApplication(sys.argv)
    > qt4reactor.install(app)
    > MainWindow = QtGui.QMainWindow()
    > win = Window(MainWindow)
    > MainWindow.show()
    > from twisted.internet import reactor
    > reactor.run()
    > # <<<<<<<<
    > my window shows and run correctly.
    >
    > If I run this:
    > # >>>>>>>>>>>>>
    > import qt4reactor
    > import sys
    > from PyQt4 import QtGui
    > from winIum import Window
    >
    > def creApp():
    > app = QtGui.QApplication(sys.argv)
    > qt4reactor.install(app)
    > retrun app
    > def creWin():
    > MainWindow = QtGui.QMainWindow()
    > win = Window(MainWindow)
    > MainWindow.show()
    > def main():
    > app = creApp()
    > creWin()
    > from twisted.internet import reactor
    > reactor.run()
    > # <<<<<<<<<<<<<
    > my window doesn't show and the script doesn't stop but remains trapped in
    > some gui loop.
    >
    > What's the problem I can't see??
    >
    > Thank you in advance for any help.
    > Licia


    Well the only problem according to me could be with the 'from
    twisted.internet import reactor' statement.
    Try putting this at the beginning of the script. Might be it will work
    just fine...
     
    ArdPy, Nov 4, 2006
    #2
    1. Advertising

  3. On Sat, 04 Nov 2006 13:02:26 +0100, IloChab wrote:


    > If I run this:
    > # >>>>>>>>>>>>>
    > import qt4reactor
    > import sys
    > from PyQt4 import QtGui
    > from winIum import Window
    >
    > def creApp():
    > app = QtGui.QApplication(sys.argv)
    > qt4reactor.install(app)
    > retrun app


    This is not actually the code you are trying to run, because "retrun app"
    would give a SyntaxError. You should copy and paste the actual code you
    have run, don't retype it.


    > my window doesn't show and the script doesn't stop but remains trapped
    > in some gui loop.
    >
    > What's the problem I can't see??


    Don't know, I can't see it either.

    Why don't you add some temporary print statements into your code to try to
    narrow it down?


    def main():
    print "entered main"
    app = creApp()
    print "done creApp"
    creWin()
    print "done creWin"
    from twisted.internet import reactor
    print "done import"
    reactor.run()
    print "done reactor.run; exiting main"


    --
    Steven.
     
    Steven D'Aprano, Nov 4, 2006
    #3
  4. IloChab

    IloChab Guest

    Il Sun, 05 Nov 2006 04:19:36 +1100, Steven D'Aprano ha scritto:

    > Why don't you add some temporary print statements into your code to try
    > to narrow it down?
    >
    >

    I did it and I followed the script with the dubugger and what I saw is
    that it gets lost in the simulate method of QTReactor instead if executing
    QTapplication.
    So I thought about some wrong rebinding of 'app' in the multiple functions
    call.
    I was guessing about some python error that I could not see.

    But the fact that neither you can't see anything wrong, let me guess about
    some problem with the reactor.

    Thank for your time.
    Licia
     
    IloChab, Nov 5, 2006
    #4
  5. IloChab <> wrote:
    [this works]
    >def main():
    > app = QtGui.QApplication(sys.argv)
    > qt4reactor.install(app)
    > MainWindow = QtGui.QMainWindow()
    > win = Window(MainWindow)
    > MainWindow.show()
    > from twisted.internet import reactor
    > reactor.run()

    [this doesn't]
    >def creApp():
    > app = QtGui.QApplication(sys.argv)
    > qt4reactor.install(app)
    > retrun app
    >def creWin():
    > MainWindow = QtGui.QMainWindow()
    > win = Window(MainWindow)
    > MainWindow.show()
    >def main():
    > app = creApp()
    > creWin()
    > from twisted.internet import reactor
    > reactor.run()


    I don't know if this is the problem or not (knowing neither Qt nor
    Twisted), but creWin() creates a window (or two) then throws it
    (them?) away on returning to main() (I assume you've chopped
    off the bit where main() is actually called). So it's not too
    surprising your window doesn't show: by the time you get to
    running anything, you don't have a window object to show. (Unless
    a Qt application object is a discoverable global and windows
    inject a reference to themselves into it.)

    --
    \S -- -- http://www.chaos.org.uk/~sion/
    ___ | "Frankly I have no feelings towards penguins one way or the other"
    \X/ | -- Arthur C. Clarke
    her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
     
    Sion Arrowsmith, Nov 6, 2006
    #5
  6. IloChab

    David Boddie Guest

    Sion Arrowsmith wrote:

    > I don't know if this is the problem or not (knowing neither Qt nor
    > Twisted), but creWin() creates a window (or two) then throws it
    > (them?) away on returning to main() (I assume you've chopped
    > off the bit where main() is actually called). So it's not too
    > surprising your window doesn't show: by the time you get to
    > running anything, you don't have a window object to show.


    It seems to me that your analysis is correct. The first example
    works as expected because MainWindow is still in scope when
    the reactor is run.

    def main():
    app = QtGui.QApplication(sys.argv)
    qt4reactor.install(app)
    MainWindow = QtGui.QMainWindow()
    win = Window(MainWindow)
    MainWindow.show()
    from twisted.internet import reactor
    reactor.run()

    However, in the second example, MainWindow is created as a local
    variable inside creWin() and is deleted when that function returns.

    def creWin():
    MainWindow = QtGui.QMainWindow()
    win = Window(MainWindow)
    MainWindow.show()
    def main():
    app = creApp()
    creWin()
    from twisted.internet import reactor
    reactor.run()

    By the time the reactor runs, there's no window (open or otherwise)
    so the application will never exit the Qt event loop (or whatever
    actually runs when Twisted is involved).

    > (Unless a Qt application object is a discoverable global and
    > windows inject a reference to themselves into it.)


    Well, you can access the application's instance, once it's been set
    up, via the qApp global variable in the PyQt4.QtGui module. However,
    you can't take advantage of Qt's parent-based object ownership
    mechanism to get round this issue with local variables because
    widgets need to be given QWidget parents, and QApplication is not a
    QWidget subclass.

    The original poster should either use a global MainWindow variable or
    encapsulate the functions in a class.

    David
     
    David Boddie, Nov 6, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Amir
    Replies:
    3
    Views:
    599
  2. nc
    Replies:
    1
    Views:
    503
    nice.guy.nige
    Feb 3, 2005
  3. Replies:
    2
    Views:
    340
  4. Jeannie
    Replies:
    15
    Views:
    893
    Jeannie
    Aug 30, 2005
  5. =?Utf-8?B?TFc=?=
    Replies:
    8
    Views:
    427
    =?Utf-8?B?TFc=?=
    Apr 25, 2007
Loading...

Share This Page