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

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

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

    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_RestoreT hread: 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(QMa inWindow):

    def __init__(self, app, *args):
    QMainWindow.__i nit__(self, *args)
    self.app = app #### this seems to be the problem ####
    self.button = QPushButton(sel f.app.knopftext , self)
    # self.button = QPushButton(qAp p.knopftext, self) #### won't run at
    all ####
    self.setCentral Widget(self.but ton)
    self.connect(se lf.button, SIGNAL('clicked ()'), self,
    SLOT('close()') )

    class HelloApp(QAppli cation):

    def __init__(self, *args):
    QApplication.__ init__(self, *args)
    self.knopftext = 'HelloApp'
    self.connect(se lf, SIGNAL('lastWin dowClosed()'),
    self, SLOT('quit()'))
    self.MainWindow = HelloWindow(sel f)
    self.setMainWid get(self.MainWi ndow)
    self.MainWindow .show()

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

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

    Thank you,
    Koczian

    --
    Dr. Sibylle Koczian
    Universitaetsbi bliothek, Abt. Naturwiss.
    D-86135 Augsburg

    Tel.: (0821) 598-2400, Fax : (0821) 598-2410
    e-mail : Sibylle.Koczian @Bibliothek.Uni-Augsburg.DE

  • Phil Thompson

    #2
    Re: PyQt, main window can't have reference to application class

    On Monday 24 May 2004 3:54 pm, Sibylle Koczian wrote:[color=blue]
    > 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_RestoreT hread: NULL tstate".[/color]

    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.
    [color=blue]
    > 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.[/color]

    It's the same C++ instance, but they are different types. knopftext is an
    attribute of the HelloApp type. qApp is of type QApplication.
    [color=blue]
    > 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(QMa inWindow):
    >
    > def __init__(self, app, *args):
    > QMainWindow.__i nit__(self, *args)
    > self.app = app #### this seems to be the problem ####
    > self.button = QPushButton(sel f.app.knopftext , self)
    > # self.button = QPushButton(qAp p.knopftext, self) #### won't run at
    > all ####
    > self.setCentral Widget(self.but ton)
    > self.connect(se lf.button, SIGNAL('clicked ()'), self,
    > SLOT('close()') )
    >
    > class HelloApp(QAppli cation):
    >
    > def __init__(self, *args):
    > QApplication.__ init__(self, *args)
    > self.knopftext = 'HelloApp'
    > self.connect(se lf, SIGNAL('lastWin dowClosed()'),
    > self, SLOT('quit()'))
    > self.MainWindow = HelloWindow(sel f)
    > self.setMainWid get(self.MainWi ndow)
    > self.MainWindow .show()
    >
    > def main(args):
    > app = HelloApp(args)
    > app.exec_loop()
    >
    > if __name__ == '__main__':
    > main(sys.argv)
    >
    > Thank you,
    > Koczian[/color]

    Phil

    Comment

    • Sibylle Koczian

      #3
      Re: PyQt, main window can't have reference to application class

      Phil Thompson schrieb:[color=blue]
      > On Monday 24 May 2004 3:54 pm, Sibylle Koczian wrote:
      >[color=green]
      >>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_RestoreT hread: NULL tstate".[/color]
      >
      >
      > 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.
      >[/color]
      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.
      [color=blue]
      >[color=green]
      >>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.[/color]
      >
      >
      > It's the same C++ instance, but they are different types. knopftext is an
      > attribute of the HelloApp type. qApp is of type QApplication.
      >[/color]
      That's what I meant.

      Thank you,
      Koczian

      --
      Dr. Sibylle Koczian
      Universitaetsbi bliothek, Abt. Naturwiss.
      D-86135 Augsburg

      Tel.: (0821) 598-2400, Fax : (0821) 598-2410
      e-mail : Sibylle.Koczian @Bibliothek.Uni-Augsburg.DE

      Comment

      Working...