Really strange behavior

I

IloChab

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
 
A

ArdPy

IloChab said:
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...
 
S

Steven D'Aprano

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"
 
I

IloChab

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
 
S

Sion Arrowsmith

IloChab said:
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.)
 
D

David Boddie

Sion said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top