PyQt, main window can't have reference to application class

S

Sibylle Koczian

Still trying to learn PyQt from a book about several Python GUI
toolkits, I seem to learn first what doesn't work. The following small
script seems to work, but after closing the window I get the error
message "Fatal Python error: PyEval_RestoreThread: NULL tstate".

Without the line "self.app = app" the error goes away. So I suppose the
main window can't have a reference to the application class. Right?
Using the global variable qApp instead of self.app doesn't work either:
qApp seems to be an instance of QApplication, not an instance of HelloApp.

Should the GUI independent actions of the application rather be methods
of the main window? Or methods of some class not derived from QObject,
with a reference to an instance of this class in the main window
instance and not in the application instance? Comparing with Delphi I
suppose this might be the better way. I'd like to use the same class
with different GUI toolkits (Tkinter, wxPython), so all the GUI
independent stuff should be separate.

#!/usr/bin/python
# -*- coding: latin-1 -*-
# hello2.py

import sys
from qt import *

class HelloWindow(QMainWindow):

def __init__(self, app, *args):
QMainWindow.__init__(self, *args)
self.app = app #### this seems to be the problem ####
self.button = QPushButton(self.app.knopftext, self)
# self.button = QPushButton(qApp.knopftext, self) #### won't run at
all ####
self.setCentralWidget(self.button)
self.connect(self.button, SIGNAL('clicked()'), self,
SLOT('close()'))

class HelloApp(QApplication):

def __init__(self, *args):
QApplication.__init__(self, *args)
self.knopftext = 'HelloApp'
self.connect(self, SIGNAL('lastWindowClosed()'),
self, SLOT('quit()'))
self.MainWindow = HelloWindow(self)
self.setMainWidget(self.MainWindow)
self.MainWindow.show()

def main(args):
app = HelloApp(args)
app.exec_loop()

if __name__ == '__main__':
main(sys.argv)

Thank you,
Koczian

--
Dr. Sibylle Koczian
Universitaetsbibliothek, Abt. Naturwiss.
D-86135 Augsburg

Tel.: (0821) 598-2400, Fax : (0821) 598-2410
e-mail : (e-mail address removed)-Augsburg.DE
 
P

Phil Thompson

Still trying to learn PyQt from a book about several Python GUI
toolkits, I seem to learn first what doesn't work. The following small
script seems to work, but after closing the window I get the error
message "Fatal Python error: PyEval_RestoreThread: NULL tstate".

You have created a circular reference which won't help. The problem doesn't
occur if PyQt is built with SIP v4 - it uses the newer Python thread API.
Without the line "self.app = app" the error goes away. So I suppose the
main window can't have a reference to the application class. Right?
Using the global variable qApp instead of self.app doesn't work either:
qApp seems to be an instance of QApplication, not an instance of HelloApp.

It's the same C++ instance, but they are different types. knopftext is an
attribute of the HelloApp type. qApp is of type QApplication.
Should the GUI independent actions of the application rather be methods
of the main window? Or methods of some class not derived from QObject,
with a reference to an instance of this class in the main window
instance and not in the application instance? Comparing with Delphi I
suppose this might be the better way. I'd like to use the same class
with different GUI toolkits (Tkinter, wxPython), so all the GUI
independent stuff should be separate.

#!/usr/bin/python
# -*- coding: latin-1 -*-
# hello2.py

import sys
from qt import *

class HelloWindow(QMainWindow):

def __init__(self, app, *args):
QMainWindow.__init__(self, *args)
self.app = app #### this seems to be the problem ####
self.button = QPushButton(self.app.knopftext, self)
# self.button = QPushButton(qApp.knopftext, self) #### won't run at
all ####
self.setCentralWidget(self.button)
self.connect(self.button, SIGNAL('clicked()'), self,
SLOT('close()'))

class HelloApp(QApplication):

def __init__(self, *args):
QApplication.__init__(self, *args)
self.knopftext = 'HelloApp'
self.connect(self, SIGNAL('lastWindowClosed()'),
self, SLOT('quit()'))
self.MainWindow = HelloWindow(self)
self.setMainWidget(self.MainWindow)
self.MainWindow.show()

def main(args):
app = HelloApp(args)
app.exec_loop()

if __name__ == '__main__':
main(sys.argv)

Thank you,
Koczian

Phil
 
S

Sibylle Koczian

Phil said:
You have created a circular reference which won't help. The problem doesn't
occur if PyQt is built with SIP v4 - it uses the newer Python thread API.
But the circular reference would remain, wouldn't it? I still don't see
a way around it: the main window must know about the GUI independent
methods of the application, and those methods should be able to exchange
data with the main window. But I still want to keep the GUI independent
things separate.
It's the same C++ instance, but they are different types. knopftext is an
attribute of the HelloApp type. qApp is of type QApplication.
That's what I meant.

Thank you,
Koczian

--
Dr. Sibylle Koczian
Universitaetsbibliothek, Abt. Naturwiss.
D-86135 Augsburg

Tel.: (0821) 598-2400, Fax : (0821) 598-2410
e-mail : (e-mail address removed)-Augsburg.DE
 

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,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top