mobcdi
mobcdi

Reputation: 1592

Launch external application from wxPython

Is it possible to launch another application from within a wxPython app? For example if I have a list of pdf files can the user double click on 1 of them to have the users registered pdf file application to open up and display the contents?

Upvotes: 2

Views: 402

Answers (2)

Rolf of Saxony
Rolf of Saxony

Reputation: 22443

wx.LaunchDefaultBrowser(url, flags=0) is the feature that you are looking for.
i.e.

import  wx

class MyPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, id=-1)
        sizer = wx.BoxSizer(wx.VERTICAL)
        btn = wx.Button(self, wx.NewId(), "Open PDF File",size=(20,50))
        self.Bind(wx.EVT_BUTTON, self.OnOpenButton, btn)
        sizer.Add(btn, 0, flag=wx.EXPAND|wx.ALL)
        self.SetSizer(sizer)

    def OnOpenButton(self, event):
        dlg = wx.FileDialog(self, wildcard="*.pdf")
        if dlg.ShowModal() == wx.ID_OK:
            url = dlg.GetPath()
        dlg.Destroy()
        try:
            if not url:
                return
        except:
            return
        wx.LaunchDefaultBrowser(url)

app = wx.App()
frame = wx.Frame(None, -1, "PDF Default Browser", size = (640, 480))
MyPanel(frame)
frame.Show(True)
app.MainLoop()

Upvotes: 1

Mike Driscoll
Mike Driscoll

Reputation: 33071

I would recommend using Python's os module can just call os.startfile(path). You could also use the subprocess module for this too.

For your second question about a file picker, see probably want wx.FileDialog which you can read more about here:

Upvotes: 2

Related Questions