Restarting a program

I have a program which initiates a logon sequence when an instance of the main
window class is created. All i'm trying to do, is beable to destroy the main
window then restart it.

class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        super(MainWindow, self).__init__(parent, wx.ID_ANY, title,
            size=wx.Size(1024, 768))
        self._create_log_on()

    .....
    .....
    .....

    def _logoff(self):
        window_restart()

def window_restart():
    view.Close(True)
    view = MainWindow(None, "Title")

if __name__ == "__main__":
    app = wx.PySimpleApp()
    view = MainWindow(None, "Title")
    app.MainLoop()

Now, when I call my self._logoff method, it correctly calls window_restart()
but I get "UnboundLocalError: local variable 'view' referenced before
assignment" on the view.Close(True) line. Surely view would still be
available within window_restart?

I have a feeling I should probably be doing this in a different way anyway, I
don't want to kill the app, just destroy the current MainWindow instance and
create a new one.

Many Thanks
Caig

Hi Craig,

I have a program which initiates a logon sequence when an instance of the main
window class is created. All i'm trying to do, is beable to destroy the main
window then restart it.

class MainWindow(wx.Frame):
   def __init__(self, parent, title):
       super(MainWindow, self).__init__(parent, wx.ID_ANY, title,
           size=wx.Size(1024, 768))
       self._create_log_on()

   .....
   .....
   .....

   def _logoff(self):
       window_restart()

def window_restart():
   view.Close(True)
   view = MainWindow(None, "Title")

if __name__ == "__main__":
   app = wx.PySimpleApp()
   view = MainWindow(None, "Title")
   app.MainLoop()

Now, when I call my self._logoff method, it correctly calls window_restart()
but I get "UnboundLocalError: local variable 'view' referenced before
assignment" on the view.Close(True) line. Surely view would still be
available within window_restart?

I have a feeling I should probably be doing this in a different way anyway, I
don't want to kill the app, just destroy the current MainWindow instance and
create a new one.

I don't know if the code you sent actually is exactly the code you are
using, but:

def window_restart():
   view.Close(True)
   view = MainWindow(None, "Title")

the window_restart method has no input arguments, how could Python
know what "view" is? In other words, in the local namespace of
window_restart method no variables have been defined, so obviously
Python is throwing an UnboundLocalError. You could save a reference of
"view" and then modify your method as follows:

def window_restart():
   self.view.Close(True)
   self.view = MainWindow(None, "Title")

Or you could pass "view" as an argument to your window_restart method.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

···

On 10/8/07, Craig Douglas wrote: