Pop-up PDF viewer

I’m using this script to display a pdf file:

import wx
import wx.lib.sized_controls as sc

from wx.lib.pdfviewer import pdfViewer, pdfButtonPanel

class PDFViewer(sc.SizedFrame):
    def __init__(self, parent, **kwds):
        super(PDFViewer, self).__init__(parent, **kwds)

        paneCont = self.GetContentsPane()
        self.buttonpanel = pdfButtonPanel(paneCont, wx.NewId(),
                                wx.DefaultPosition, wx.DefaultSize, 0)
        self.viewer = pdfViewer(paneCont, wx.NewId(), wx.DefaultPosition,
        self.viewer.UsePrintDirect = False
        self.viewer.SetSizerProps(expand=True, proportion=1)

        # introduce buttonpanel and viewer to each other
        self.buttonpanel.viewer = self.viewer
        self.viewer.buttonpanel = self.buttonpanel

if __name__ == '__main__':
    import wx.lib.mixins.inspection as WIT
    app = WIT.InspectableApp(redirect=False)

    pdfV = PDFViewer(None, size=(800, 600))
    pdfV.viewer.UsePrintDirect = False
    pdfV.viewer.LoadFile(r"F:\\user guide.pdf")


As a standalone app it works perfectly. However I want to open it from a button in another python script in which case “if name == ‘main’” is going to scupper me. I don’t quite see how to re-structure the script to make it behave like a simple message dialog.

You can use multiple modules in your own code just like you are using modules from wxPython or the Python standard library. Namely, you import the module, or items from the module, and then create instances or call functions as needed. In your case your other module can import this one, and then in the button event handler create an instance of PDFViewer and Show it.

Thanks Robin, I am aware of this. I use external modules all the time. Sorry, I may not have phrased the question very well. Normally I would import the module at the top of a script then call it with something like: dlg = pdf.PDFViewer(self).
My problem here is that the code that opens the pdf document is under the ‘if __name’ statement thereby causing it to be ignored when the script is called by another script. If I remove this ‘if’ statement the script is run immediately by the import command in the parent script.

Maybe you should describe what you actually want to accomplish.
Is the other ‘script’ a wxPython program? Is it really a ‘script’ running in the console which should pause while the PDF is displayed? Is it supposed to start the PDF viewer to run independently?

But anyway, this is a very, very basic Python question.

I know its very basic, which is why its so frustrating.
I am running a python program. At the click of a button I want the PDFViewer to open and display
F:\user guide.pdf in an independent floating window.

Then call the pdf viewer script, e.g. using subprocess.popen.

That’s overkill.
Its a python app opening another python module. I should be able to open it in the same way as a dialog.
I just need to know how to restructure PDFViewer so it will work when name isn’t main!

Using subprocess will cause the PDFViewer to be opened by whatever app is associated with py files, which will fail if this is packaged for use on a machine without a python installation.

Well, if there’s code “under” main that you don’t
(anymore) want to run when code under main is run, then
put it elsewhere. Then call elsewhere() when you do need that
code to run. That is what Dietmar meant with “basic Python

If you the still want that code to also run when code under
main is run then call elsewhere() from main, too.


It’s not difficult or tricky. I suspect that you are overthinking things. You just need to put the code you want to be run in the button’s event handler. In your example that would be the code from if __main__... that creates and shows the PDFViewer, minus the app related things.