Search code examples
pythonwxpythonexeupdates

Reopen wxpython exe when closed


I have an exe that I created using wxPython (with PyInstaller). Within the code, I have the ability to download an update to the exe (thus downloading a copy of the exe). Folks have this exe "installed" on their computer in the folder C:\Program Files\MyProgram\MyProg.exe I use urllib.urlretrieve(url,'MyProg.exe') to download the new file. It download correctly but now I want to automatically close my current window and re-open the exe (the new downloaded version).

My Steps:

  1. Click "Update" (I have this set up)
  2. Close current window/program
  3. Download new exe (overwrites current exe)
  4. Open newly downloaded exe

My program closes but nothing happens. If I take out self.Close(), the program will download a new file only if I save it to a different name. I can then run the new saved file, but my current window stays open.

Any thoughts?

My code:

class TMainForm(wx.Frame):

    def __init__(self, *args, **kwds):
        ....

    def OnProgramUpdate(self):

        dlg = wx.MessageDialog(self,
            "Update?",
            "Update", wx.YES_NO|wx.YES_DEFAULT|wx.ICON_QUESTION)
        closeresult = dlg.ShowModal()
        dlg.Destroy()
        if closeresult == wx.ID_YES:
            self.Close()
            urllib.urlretrieve(url,'MyProg.exe')
            p = subprocess.Popen("MyProg.exe")



class TApplication(wx.App):
    def OnInit(self):

            wx.InitAllImageHandlers()
            MainForm = TMainForm(None, -1,"")
            self.SetTopWindow(MainForm)

            MainForm.Show()
            return 1

if __name__ == "__main__":
    Application = TApplication(0)
    Application.MainLoop()

Solution

  • You need to write out another file, e.g.: a batch file, that waits for a time then copies the temporary, downloaded file over the existing file and then restarts it,and deletes itself. Then call it asynchronously, i.e. not waiting for it to finish then exit leaving it to run.