Vizard 8 » Tutorials & Examples » Python Programming » Addons » wxPython
8.1

wxPython

wxPython is a Python library for creating GUI applications.

Installation

Use wxPython

You should now be able to use the wxPython library within Vizard. Browse through the website or the sample scripts to learn how to use the module.

 

Once you have some experience using wxPython you can start embedding your Vizard scripts into a wxPython GUI application. The biggest difference between a standard Vizard script and an embedded Vizard script is that you must manually control the Vizard mainloop within the embedded version.

Example

The sample script below shows how to embed the Vizard graphics window within a wxPython application. The key differences are noted with the IMPORTANT comments:

import wx
import viz

class VizardFrame(wx.Frame):
    """A simple frame that hosts the Vizard graphics window"""
    def __init__(self, parent):
        wx.Frame.__init__(  self,
                            parent,
                            -1,
                            "Vizard Embedded Example",
                            size=(800,600),
                            style=wx.DEFAULT_FRAME_STYLE )

        # Create a window to render Vizard 3D graphics
        window = wx.Window(self, -1)

        # Disable escape key from exiting Vizard and let WxPython handle exiting
        viz.setOption('viz.default_key.quit', 0)

        """
        IMPORTANT:
            You must pass the viz.EMBEDDED flag when embedding
            the graphics loop within the script.
            You must also pass the raw window handle when
            embedding the graphics window within an existing window.
        """
        viz.go(viz.EMBEDDED, window.GetHandle())

        # Add a simple rotating quad
        quad = viz.addTexQuad( pos=(0,1.6,5) )
        quad.color(viz.RED)
        quad.disable(viz.LIGHTING)
        quad.addAction(vizact.spin(0,1,0,90))

        """
        IMPORTANT:
            You must setup a timer to update the Vizard graphics loop
            at a fast enough rate. (10 milliseconds in this example)
        """
        self.timer = wx.Timer(self)
        self.timer.Start(10)
        self.Bind(wx.EVT_TIMER, self.OnTimer)

        # Setup event handler for window closing
        self.Bind(wx.EVT_CLOSE, self.OnClose)

    def OnClose(self, event):
        """Stop timer and destroy window on close"""
        self.timer.Stop()
        self.Destroy()

    def OnTimer(self, event):
        """Update the Vizard graphics loop whenever the window timer expires"""
        viz.frame()

if __name__ == '__main__':

    # Setup a simple wxPython app and start the mainloop
    app = wx.App(False)
    frame = VizardFrame(None)
    frame.Show(True)
    app.MainLoop()