Really strange behavior

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • IloChab

    Really strange behavior

    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:

    Now,
    if I run this:
    # >>>>>>>>>
    import qt4reactor
    import sys
    from PyQt4 import QtGui
    from winIum import Window

    def main():
    app = QtGui.QApplicat ion(sys.argv)
    qt4reactor.inst all(app)
    MainWindow = QtGui.QMainWind ow()
    win = Window(MainWind ow)
    MainWindow.show ()
    from twisted.interne t 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.QApplicat ion(sys.argv)
    qt4reactor.inst all(app)
    retrun app
    def creWin():
    MainWindow = QtGui.QMainWind ow()
    win = Window(MainWind ow)
    MainWindow.show ()
    def main():
    app = creApp()
    creWin()
    from twisted.interne t 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
  • ArdPy

    #2
    Re: Really strange behavior


    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:

    Now,
    if I run this:
    # >>>>>>>>>
    import qt4reactor
    import sys
    from PyQt4 import QtGui
    from winIum import Window
    >
    def main():
    app = QtGui.QApplicat ion(sys.argv)
    qt4reactor.inst all(app)
    MainWindow = QtGui.QMainWind ow()
    win = Window(MainWind ow)
    MainWindow.show ()
    from twisted.interne t 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.QApplicat ion(sys.argv)
    qt4reactor.inst all(app)
    retrun app
    def creWin():
    MainWindow = QtGui.QMainWind ow()
    win = Window(MainWind ow)
    MainWindow.show ()
    def main():
    app = creApp()
    creWin()
    from twisted.interne t 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.interne t import reactor' statement.
    Try putting this at the beginning of the script. Might be it will work
    just fine...

    Comment

    • Steven D'Aprano

      #3
      Re: Really strange behavior

      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.QApplicat ion(sys.argv)
      qt4reactor.inst all(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.interne t import reactor
      print "done import"
      reactor.run()
      print "done reactor.run; exiting main"


      --
      Steven.

      Comment

      • IloChab

        #4
        Re: Really strange behavior

        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

        Comment

        • Sion Arrowsmith

          #5
          Re: Really strange behavior

          IloChab <IloChab@yahoo. itwrote:
          [this works]
          >def main():
          app = QtGui.QApplicat ion(sys.argv)
          qt4reactor.inst all(app)
          MainWindow = QtGui.QMainWind ow()
          win = Window(MainWind ow)
          MainWindow.show ()
          from twisted.interne t import reactor
          reactor.run()
          [this doesn't]
          >def creApp():
          app = QtGui.QApplicat ion(sys.argv)
          qt4reactor.inst all(app)
          retrun app
          >def creWin():
          MainWindow = QtGui.QMainWind ow()
          win = Window(MainWind ow)
          MainWindow.show ()
          >def main():
          app = creApp()
          creWin()
          from twisted.interne t 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 -- siona@chiark.gr eenend.org.uk -- 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

          Comment

          • David Boddie

            #6
            Re: Really strange behavior

            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.QApplicat ion(sys.argv)
            qt4reactor.inst all(app)
            MainWindow = QtGui.QMainWind ow()
            win = Window(MainWind ow)
            MainWindow.show ()
            from twisted.interne t 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.QMainWind ow()
            win = Window(MainWind ow)
            MainWindow.show ()
            def main():
            app = creApp()
            creWin()
            from twisted.interne t 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

            Comment

            Working...