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

Sibylle Koczian Sibylle.Koczian at Bibliothek.Uni-Augsburg.de
Mon May 24 10:54:07 EDT 2004


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 : Sibylle.Koczian at Bibliothek.Uni-Augsburg.DE




More information about the Python-list mailing list